Skip to content Skip to sidebar Skip to footer

Some Help Understanding Window Object

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 wind

Solution 1:

The window object is effectively two things:

  1. 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// Returns true
  2. 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!

Post a Comment for "Some Help Understanding Window Object"