JavaScript: Window object

Sahar E.Hassan
Oct 19, 2020
window object

Window object:

It’s the global object that represents the open window in the browser.

  • 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.

BOM (Browser Object Model): it contains the data about the browser rather than the document to be rendered.

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.

and there’re much more properties you can use.(check MDN)

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.

and there’re much more methods you can use.(check MDN)

Thanks for reading and hope you enjoyed the article. Waiting your comments and feedback. 👋

Happy coding.

--

--