Skip to main content

window

Small wrappers around common window operations โ€” opening/closing/focusing windows, scrolling, reloading, and reading or watching viewport size.

Import: @rtorcato/browser-common/window

๐Ÿ“– MDN: Window

Browser supportโ€‹

APIChromeEdgeFirefoxSafariiOS SafariBaseline
Window.scrollToโœ… 1+โœ… 12+โœ… 1+โœ… 1+โœ… 1+Widely available
Window.openโœ… 1+โœ… 12+โœ… 1+โœ… 1+โ— partial 1+ [1]Limited
  1. iOS Safari: This method will not function if the target parameter is unspecified or set to _blank.

โœ… supported ยท โ— partial ยท โŒ not supported ยท [n] see note. Generated from MDN browser-compat-data and web-features. caniuse: Window.scrollTo ยท Window.open

Exampleโ€‹

import {
openWindow,
closeWindow,
focusWindow,
blurWindow,
scrollToTop,
scrollToBottom,
reloadWindow,
getWindowSize,
onWindowResize,
} from '@rtorcato/browser-common/window'

openWindow('https://example.com', '_blank')
scrollToTop('smooth')

const { width, height } = getWindowSize()
const off = onWindowResize(() => console.log(window.innerWidth))
off()

Exportsโ€‹

  • openWindow(url, target?, features?) โ€” window.open; returns the new Window or null if blocked
  • closeWindow() โ€” closes the current window
  • focusWindow() โ€” focuses the current window
  • blurWindow() โ€” blurs the current window
  • scrollToTop(behavior?) โ€” scrolls to the top ('auto' or 'smooth')
  • scrollToBottom(behavior?) โ€” scrolls to document.body.scrollHeight
  • reloadWindow() โ€” reloads the current page
  • getWindowSize() โ€” { width, height } from innerWidth/innerHeight
  • onWindowResize(callback) โ€” subscribe to resize; returns an unsubscribe function

See the API reference for full signatures.