JavaScript: Window object

window object

Window object:

  • All predefined methods like: alert(“hello”) are attached to the global window object.
alert("hello");     ===     window.alert("hello");
  • All new global variables and methods are also attached to the global window object.
var name = "John";
console.log(name); //John
console.log(window.name); //John
  • BOM (Browser Object Model) data can be accessed using window object.

Window object Properties:

  • window.document: it returns a reference to the document that the window contains
  • window.innerWidth: it returns the width of the content area of the browser window.
  • window.innerHeight: it returns the height of the content area of the browser window.
  • window.event: it returns the event that is currently being handled by the javascript code’s context, or undefined if no event is currently being handled.
  • window.history: it returns a reference to the history object.
  • window.name: it sets/gets the name of the window.

Window object Methods:

  • window.alert(): it display the default alert dialog.
  • window.confirm(): it displays a dialog with a message that the user needs to respond to.
  • window.open(): it opens a new window.
  • window.scroll(): it scrolls the window to a particular place in the document.
  • window.find(): it searches for a given string in a window.
  • window.print(): it opens the print dialog to print the current document.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store