JavaScript Window - Browser Object Model
The Browser Object Model (BOM) enables JavaScript to "talk" with the browser.
Browser Object Model (BOM)
Browser Object Model (Browser Object Model (BOM)) has no formal standard yet.
Since modern browsers have (almost) implemented the same methods and properties for JavaScript interaction, these are often considered BOM methods and properties.
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 one of the properties of the window object:
window.document.getElementById("header");
The same as:
document.getElementById("header");
Window size
There are three ways to determine the size of the browser window.
For Internet Explorer, Chrome, Firefox, Opera, and Safari:
- window.innerHeight - the internal height of the browser window (including scrollbars)
- window.innerWidth - the internal 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
;
Try it »
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