JS: Browser Window Object
when doing web dev, there are 3 kinds of objects you work with:
- JavaScript Standard Objects and user defined objects. e.g. function, array, date, regex, etc. [see JS: Object Overview]
- Browser's window object. Represented by keyword
window
in browser. (This is called “host object” by JavaScript spec.) - Document Object. Represented by keyword
window.document
.
The Window Object
The browser's window
object is
the Global Object.
All JavaScript objects
are properties of the window
object.
In browser console, type window
to view this object's properties. [see How to Use Browser Console]
The window
object has critical properties.
most has to do with a browser, but some has nothing to do with browser but is not in standard JavaScript programing language spec.
Examples of important functions:
- ƒ
window.alert
→ show a popup window. - ƒ
window.open
[see JS: Open URL] - ƒ
window.setTimeout
→ call a function after a given delay. - ƒ
window.setInterval
→ call a function periodically. [see SVG Clock] - ƒ
window.encodeURI
[see Encode/Decode URL tutorial] - value
window.innerWidth
. [see Find Window Size]
window.localStorage
[see Web Storage]window.history
window.location
[see Get URL (window.location)]
You don't need to prefix window.
to use its methods or properties. e.g. alert("3")
is same as window.alert("3")
The window object also has a property key "window"
,
so window.window
and
window.window.window
are the same as just
window
.
Browser Object Model
The window object and its properties and their behavior is sometimes called the Browser Object Model (aka BOM).
The Document Object (Document Object Model)
One of the most important property of window
is window.document
. This is the document object.
The document object represent the HTML/XML document in the browser window.
The document object contains methods to manipulate HTML elements and styles. This object system is called DOM (Document Object Model).
The DOM allows you to:
- Insert element.
- Remove element.
- Change element's attributes. (such as class, id, name, etc.)
- Change element's content.
- Change element's CSS style.
[see JS: DOM Methods]