The Window’s Location Object

An alternative to using Navigate is to change the HRef property of the Window object’s Location object. The effect is the same. The browser window always shows the page at some URL—so it makes sense to have some way to represent the various properties of this spot on the World Wide Web or on an Intranet. The Location object is the way we do it.

Paying attention to this object now will save us some time later— the properties of the Location object are reused more than once in the rest of the browser object model. You’ll find that the Document object has its own Location property, and that all of the links on a page are represented by a set of properties that mimic the properties of Window’s Location object.

The mother of all Location properties is HRef. Out of the eight total properties that are a part of Location, seven are derived from the HRef property—and the eighth property is HRef itself. HRef returns the current URL for the page in the browser. If the page is the top level page of a frameset, it returns this top level URL.

In addition to reading the current location, we can change it simply by setting this property. For example, the following line of code redirects the browser to the Wrox Press home page. Window, in this instance, is optional:

Window.Location.href = ""

The remainder of Location’s properties are born from the HRef property. To illustrate what we mean, suppose we were viewing a page with the URL: 

The following table shows how each of the properties would be set (a hash mark in this context is a # in the page’s HTML):

Property Value Description
href complete URL
protocol http: URL’s protocol
host hostname and port number
hostname name of host
port 80 port number (default is 80)
pathname /asp/book.htm path after host
search ?abc any query string
hash (nothing – would contain any hash specified with #) any hash value

© 1997 by Wrox Press. All rights reserved.