Possible Duplicate:
JS Window Global Object
Can some one please help me understand how the window
object works?
I know that it is the top level object there is and that the window
object represents an open window in a browser.
Can some one please help me understand more about it; maybe a link or small explanation about the window
object? I know that it has all object properties and methods. I have bean told it can recreate by calling the window.constructor
and get its native code, also that's the only way to get an instance of the object.
I'll be happy if some one will help me get more info about it.
The window
object is effectively two things:
The global object for browser-based JavaScript. All of the native objects and methods (Array
, String
, setTimeout()
) and anything you declare outside of any function's scope goes in the window
object. To test this, try opening a JavaScript console and checking this out:
window.String === String
// spits out true
The window
object also deals with the browser window. window.innerWidth
is the window's width; window.onresize
is a function that is triggered on the window resize. Because it's the "topmost" object, you can also say things like innerWidth
to get the window's width.
In general, it's a good practice to refer to write window.location
instead of just location
. Even though they'll work a lot of the time, you'll sometimes run into situations like this (which you don't want!):
function something() {
var location = 'the moon';
location.reload(); // Should be window.location.reload()
}
In the above example, you might've meant to refresh window.location
instead of the location
scoped inside this function.
And that's the window object!