13.9 The Location Object
The location property of a window is a reference to a Location object -- a representation of the URL of the document currently being displayed in that window. The href property of the Location object is a string that contains the complete text of the URL. Other properties of this object, such as protocol, host, pathname, and search, specify the various individual parts of the URL.
In addition to its properties, the Location object can be used as if it were itself a primitive string value. If you read the value of a Location object, you get the same string as you would if you read the href property of the object (because the Location object has a suitable toString( ) method). What is far more interesting, though, is that you can assign a new URL string to the location property of a window. Assigning a URL to the Location object this way has an important side effect: it causes the browser to load and display the contents of the URL you assign. For example, you might assign a URL to the location property like this:
// If the user is using an old browser that can't display DHTML content, // redirect to a page that contains only static HTML if (parseInt(navigator.appVersion) < 4) location = "staticpage.html";
As you can imagine, making the browser load specified web pages into windows is a very important programming technique. While you might expect there to be a method you can call to make the browser display a new web page, assigning a URL to the location property of a window is the supported technique for accomplishing this end. Example 13-6, later in this chapter, includes an example of setting the location property.