window Object

window Object

The Window Object is the top object in the JavaScript Object hierarchy. Every browser window that is currently open will have a corresponding window Object. All the other Objects are children of one of the window Objects. In particular, every window is associated with a particular Web page, and the HTML structure of this page is reflected in the Window's Document Object. Every Window corresponds to some URL; that URL is reflected in the location Object. Every window has a history of the previous pages that have been displayed in that window, which are represented by the various properties of the history object.

JavaScript maintains an idea of the current window, so that almost all references to sub-objects of the current window do not need to refer to it explicitly. This is why all of our output has been done using document.write() rather than window.document.write(). Window Objects have the following interesting Methods (among others):

  • alert(msgStr) - The alert method is used to alert the user to something about which the user can do nothing. An alert dialog box contains a single OK button. The alert and confirm methods are used to display their msgStr argument in a dialog box.
  • confirm(msgStr) - The confirm dialog box is more flexible, and displays its message with both an OK and a Cancel button. If the user selects OK then the confirm method returns true, otherwise it returns false.
  • open(URL, windowName, features) & close() - You use the open Method of the Window Object when you wish to open a new browser window. The URL argument is a string representing the URL that will be loaded into that window. The windowName argument is a string that gives the new window its name. This method returns an instance of the window object representing the new window created. This method also accepts a third argument (features) that can be used to specify a wide variety of display options for the new window (such as whether or not it should display its toolbar). When the close() method is invoked from a window instance the underlying window is closed and the URL in it is unloaded.
  • prompt(msgStr1, msgStr2) - The msgStr1 is text to be displayed, and the msgStr2 is an optional argument that can be used to set a default value in the text entry field. The prompt method is used to solicit user input, in the form of a string. This method returns whatever the user typed as a string.
  • setTimeout("exp", millisecondsDelay) - setTimeout is not a delay loop but tells JavaScript to wait the specified amount of time before executing the method in the string "exp".  the amount of time (in milliseconds - 1000 milliseconds equals 1 second) BEFORE an expression is evaluated.

All these methods are used to manipulate the Window state of the browser itself.