The window object represents the browser's frame or window, in which the webpage is contained.
The window object can be used to find out what the browser is running, the pages visited, size of browser window or user screen, alter text in browser status bars, open new windows , change the page that is loaded.
The window object is a Global Object, hence its name need not be used to access its properties and methods. In fact, Global functions and variables are all created as properties of the local object.
Eg: The method alert() will produce same result as window.alert(), since window object is global object.
The window object is a global object, all variables and functions declared globally become properties an methods of window object.
In the below example, variable OS and function sayName() are defined in the global scope, which automatically places them on the window object.
Hence, the variable OS is accessible as window.OS and function sayName() is also accessible via window.sayName() .
This windows are also called as popups.Viewers might not even see these windows as most browsers offer popup advertising filters
The window object's open() method is used to open windows and close using close() method. .
Table : Move and Resize Methods
|moveBy()||moveBy(30,30)||To move the window relatively by 30 pixels along both the axes|
|moveTo()||moveTo(0,0)||Moves to top left corner of the screen.|
|resizeBy()||moveBy(50,50)||Resizes the window relatively by 50* 50 pixels.|
|resizeTo()||resizeTo(500,500)||Incase the value specified by digits is too small or too large. Value in between 1 to 21 digits won't cause Range errors.|
The basic function of the window object is to get information about the Browser window.
The Window object provides a set of method through which you can interact with the window that contains your document.
These methods should be used with caution because they take control of the browser window away from the user.
Table : Interaction with the Window
|blur()||To Unfocuses the Window||void|
|close()||To Close the window||void|
|focus()||To focus on the Window||void|
|print()||Prompts the user to print the page||void|
|scrollBy(x, y)||To Scroll the document relative to its current position||void|
|scrollTo(x, y)||To Scroll to the specifies position||void|
|stop()||To stop the document from loading||void|
In order to block unexpected popups, most browsers have a built in popup blocker. When a popup is blocked, two things can have happen:
1. Either window.open will most likely return null. This can be confirmed by checking the return value.
2. Or window.open will return an error. This can be confirmed by checking the return value and wrap the call to window.open in a try-catch block.