Skip to main content

@rtorcato/browser-common / window

window

Interfaces​

OpenWindowOptions​

Defined in: window/index.ts:2

Options for openWindow.

Properties​

features?​

optional features?: string

Defined in: window/index.ts:4

Extra window.open features, e.g. 'width=600,height=400'.

noopener?​

optional noopener?: boolean

Defined in: window/index.ts:9

Adds noopener,noreferrer so the opened page gets no window.opener handle back to this page (prevents reverse tabnabbing). window.open then returns null.

Functions​

openWindow()​

openWindow(url, target?, features?): Window | null

Defined in: window/index.ts:28

Opens a new browser window or tab with the given URL.

Example​

import { openWindow } from '@rtorcato/browser-common/window'
openWindow('https://example.com', '_blank', { noopener: true })

Parameters​

url​

string

The URL to open.

target?​

string = '_blank'

The target window name (e.g., '_blank', '_self').

features?​

string | OpenWindowOptions

Optional features string (e.g., 'width=600,height=400'), or an OpenWindowOptions object.

Returns​

Window | null

The window object, or null if blocked or opened with noopener.

Remarks​

Unlike <a target="_blank">, window.open does not imply noopener: by default the opened page can reach this one through window.opener. For untrusted URLs pass { noopener: true }; the call then returns null even when the window opens.


closeWindow()​

closeWindow(): void

Defined in: window/index.ts:50

Closes the current browser window (if allowed).

Example​

import { closeWindow } from '@rtorcato/browser-common/window'
closeWindow()

Returns​

void


focusWindow()​

focusWindow(): void

Defined in: window/index.ts:63

Focuses the current browser window.

Example​

import { focusWindow } from '@rtorcato/browser-common/window'
focusWindow()

Returns​

void


blurWindow()​

blurWindow(): void

Defined in: window/index.ts:76

Blurs (removes focus from) the current browser window.

Example​

import { blurWindow } from '@rtorcato/browser-common/window'
blurWindow()

Returns​

void


scrollToTop()​

scrollToTop(behavior?): void

Defined in: window/index.ts:90

Scrolls the window to the top.

Example​

import { scrollToTop } from '@rtorcato/browser-common/window'
scrollToTop('smooth')

Parameters​

behavior?​

ScrollBehavior = 'auto'

The scroll behavior ('auto' or 'smooth').

Returns​

void


scrollToBottom()​

scrollToBottom(behavior?): void

Defined in: window/index.ts:104

Scrolls the window to the bottom.

Example​

import { scrollToBottom } from '@rtorcato/browser-common/window'
scrollToBottom('smooth')

Parameters​

behavior?​

ScrollBehavior = 'auto'

The scroll behavior ('auto' or 'smooth').

Returns​

void


reloadWindow()​

reloadWindow(): void

Defined in: window/index.ts:118

Reloads the current browser window.

Example​

import { reloadWindow } from '@rtorcato/browser-common/window'
reloadWindow()

Returns​

void


getWindowSize()​

getWindowSize(): object

Defined in: window/index.ts:132

Gets the current window size.

Example​

import { getWindowSize } from '@rtorcato/browser-common/window'
const size = getWindowSize()

Returns​

object

The window's width and height, or { width: 0, height: 0 } outside a browser.

width​

width: number

height​

height: number


onWindowResize()​

onWindowResize(callback): () => void

Defined in: window/index.ts:148

Adds a listener for window resize events.

Example​

import { onWindowResize } from '@rtorcato/browser-common/window'
const off = onWindowResize(() => console.log(window.innerWidth))
off()

Parameters​

callback​

() => void

The callback to run on resize.

Returns​

A function to remove the event listener.

() => void