JavaScript Window - Browser Object Model
The Browser Object Model (BOM) allows JavaScript to "talk" to the browser.
Browser Object Model (BOM)
The Browser Object Model (Browser Object Model (BOM)) has no official standards.
Since modern browsers have (almost) implemented the same methods and properties for JavaScript interactivity, they are often considered as methods and properties of BOM.
Window Object
All browsers support the window object. It represents the browser window.
All JavaScript global objects, functions, and variables automatically become members of the window object.
Global variables are properties of the window object.
Global functions are methods of the window object.
Even the HTML DOM document is a property of the window object:
window.document.getElementById("header");
This is the same as:
document.getElementById("header");
Window Dimensions
There are three methods to determine the dimensions of the browser window.
For Internet Explorer, Chrome, Firefox, Opera, and Safari:
window.innerHeight - The inner height of the browser window (including scrollbars)
window.innerWidth - The inner width of the browser window (including scrollbars)
For Internet Explorer 8, 7, 6, 5:
document.documentElement.clientHeight
document.documentElement.clientWidth
Or
document.body.clientHeight
document.body.clientWidth
A practical JavaScript solution (covering all browsers, including IE8 and below):
Example
var w = window.innerWidth
|| document.documentElement.clientWidth
|| document.body.clientWidth;
var h = window.innerHeight
|| document.documentElement.clientHeight
|| document.body.clientHeight;
This example displays the height and width of the browser window.
Other Window Methods
Some other methods:
window.open() - Opens a new window
window.close() - Closes the current window
window.moveTo() - Moves the current window
window.resizeTo() - Resizes the current window