File

src/helpers/window.helper.ts

Description

A utility service for working with the window object in Angular applications.

This service provides methods to retrieve the window object, the href location object, and touch capabilities.

Example :
Inject the GetWindow service and use its methods:
constructor(private getWindow: GetWindow) { }

Or instance a new class const getWindow = new GetWindow()

const window = this.getWindow.window(); const href = this.getWindow.href(); const touch = this.getWindow.touch();]

Index

Methods

Constructor

constructor(document: Document)

Initializes a new instance of the GetWindow class.

Parameters :
Name Type Optional Description
document Document No
  • The injected DOCUMENT to get a reference to the window object in a way that's safe for SSR.

Methods

href
href()

Allow to get the href location object inside ssr

Returns : string | URL
  • The href location object.
touch
touch()

Allow to get the origin location object inside ssr

Returns : boolean
  • true if the browser supports touch events, otherwise false.
window
window()

Allow to get the window object inside ssr

Returns : | null
  • The window object.
import { DOCUMENT } from '@angular/common'
import { Injectable, Inject } from '@angular/core'

/**
 * A utility service for working with the window object in Angular applications.
 *
 * This service provides methods to retrieve the window object, the href location object, and touch capabilities.
 *
 * @example
 * Inject the GetWindow service and use its methods:
 * constructor(private getWindow: GetWindow) { }
 *
 * Or instance a new class
 * const getWindow = new GetWindow()
 *
 * const window = this.getWindow.window();
 * const href = this.getWindow.href();
 * const touch = this.getWindow.touch();]
 * @returns {void}
 */
@Injectable({
  providedIn: 'root',
})
export class GetWindow {
  /**
   * Initializes a new instance of the `GetWindow` class.
   * @constructor
   * @param {Document} document - The injected DOCUMENT to get a reference to the window object in a way that's safe for SSR.
   */
  constructor(@Inject(DOCUMENT) private document: Document) { }

  /**
   * Allow to get the window object inside ssr
   * @returns {Window | null} - The window object.
   */
  window(): Window & typeof globalThis | null {
    return this.document.defaultView
  }

  /**
   * Allow to get the href location object inside ssr
   * @returns {string | URL} - The href location object.
   */
  href(): string | URL {
    const window = this.window()
    if (window) {
      return window.location.href
    }
    return ''
  }

  /**
   * Allow to get the origin location object inside ssr
   * @returns {boolean} - `true` if the browser supports touch events, otherwise `false`.
   */
  touch(): boolean {
    const window = this.window()
    if (window) {
      return 'ontouchstart' in window || navigator.maxTouchPoints > 0
    }
    return false
  }
}

results matching ""

    No results matching ""