The Window’s Location Object
An alternative to using
Navigate is to change the
HRef property of the
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
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 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 = "http://www.wrox.com"
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):
| ||http://www.wrox.com:80/asp/book.htm?abc||complete URL|
| ||http:||URL’s protocol|
| ||www.wrox.com:80||hostname and port number|
| ||www.wrox.com||name of host|
| ||80||port number (default is 80)|
| ||/asp/book.htm||path after host|
| ||?abc||any query string|
| ||(nothing – would contain any hash specified with #)||any hash value|