Javascript window properties and methods

Share

All browsers support the window object. It symbolizes the window of the browser. All variables, functions, and objects that are global to JavaScript are immediately added as members of the window object. The window object’s characteristics include global variables. The window object’s methods are known as global functions.

Read only window object properties

Window.closed: This property indicates whether the current window is closed or not.

Window.console: Returns a reference to the console object which provides access to the browser’s debugging console.

Window.customElements: Returns a reference to the CustomElementRegistry object, which can be used to register new custom elements and get information about previously registered custom elements.

Window.crypto: Returns the browser crypto object.

Window.devicePixelRatio: Returns the ratio between physical pixels and device-independent pixels in the current display.

Window.document: Returns a reference to the document that the window contains.

Window.fullScreen: This property indicates whether the window is displayed in full screen or not.

Window.history: Returns a reference to the history object.

Window.innerHeight: Gets the height of the content area of the browser window including, if rendered, the horizontal scrollbar.

Window.innerWidth: Gets the width of the content area of the browser window including, if rendered, the vertical scrollbar.

Window.length:  Returns the number of frames in the window. See also window.frames.Window.location

Window.localStorage: Returns a reference to the local storage object used to store data that may only be accessed by the origin that created it.

Window.messageManager: Returns the message manager object for this window.

Window.scrollX: Returns the number of pixels that the document has already been scrolled horizontally.

Window.scrollY:  Returns the number of pixels that the document has already been scrolled vertically.

Window.self: Returns an object reference to the window object itself.

Window.sessionStorage: Returns a reference to the session storage object used to store data that may only be accessed by the origin that created it.

Window.speechSynthesis: Returns a SpeechSynthesis object, which is the entry point into using Web Speech API speech synthesis functionality.

window object Methods

There are plenty of methods for the window object. Among them here I have listed a few methods that we should know while working with the window object.

Window.alert(): Displays an alert dialog.

window.alert("CWS");

Window.blur(): Sets focus away from the window.

Window.close() Closes the current window.

Window.confirm(): Displays a dialog with a message that the user needs to respond to.

if (window.confirm("Are you sure?")) {
  window.open("payment.html", "Thanks for payment!");
}

Window.moveTo(): Moves the window to the specified coordinates.

function moveOrigin() {
  window.moveTo(0, 0);
}

Window.open(): Opens a new window.

window.open("https://codewithsudeep.com", "_self", "popup");

Window.postMessage(): Provides a secure means for one window to send a string of data to another window, which need not be within the same domain as the first.

Window.print(): Opens the Print Dialog to print the current document.

Window.prompt(): Returns the text entered by the user in a prompt dialog.

Window.resizeTo(): Dynamically resizes the window.

function oneForth() {
  window.resizeTo(
    window.screen.availWidth / 2,
    window.screen.availHeight / 2
  );
}

Window.scroll(): Scrolls the window to a particular place in the document.

window.scroll({
  top: 11,
  left: 11,
  behavior: 'smooth'
});

Window.scrollBy(): Scrolls the document in the window by the given amount.

Window.scrollTo(): Scrolls to a particular set of coordinates in the document.

Read JS Articles: Javascript

Share
Sudeep Mishra

Sudeep Mishra

Healing

%d bloggers like this: