Java Reference
In-Depth Information
The location Object
The location object contains lots of potentially useful information about the current page's location.
Not only does it contain the Uniform Resource Locator (URL) for the page, but also the server hosting
the page, the port number of the server connection, and the protocol used. This information is made
available through the location object's href, hostname, port, and protocol properties. However,
many of these values are only really relevant when you are loading the page from a server and not, as
you are doing in the present examples, loading the page directly from a local hard drive.
In addition to retrieving the current page's location, you can use the methods of the location object to
change the location and refresh the current page.
You can navigate to another page in two ways. You can either set the location object's href property to
point to another page, or you can use the location object's replace() method. The effect of the two is
the same; the page changes location. However, they differ in that the replace() method removes the
current page from the history stack and replaces it with the new page you are moving to, whereas using
the href property simply adds the new page to the top of the history stack. This means that if the
replace() method has been used and the user clicks the Back button in the browser, the user can't go
back to the original page loaded. If the href property has been used, the user can use the Back button
as normal.
For example, to replace the current page with a new page called myPage.htm, you'd use the replace()
method and write the following:
window.location.replace(“myPage.htm”);
This will load myPage.htm and replace any occurrence of the current page in the history stack with
myPage.htm.
To load the same page and to add it to the history of pages navigated to, you use the href property:
window.location.href = “myPage.htm”;
and the page currently loaded is added to the history. In both of the preceding cases, window is in front of
the expression, but as the window object is global throughout the page, you could have written one of the
following:
location.replace(“myPage.htm”);
location.href = “myPage.htm”;
The navigator Object
The navigator object is another object that is a property of window and is available in all browsers. Its
name is more historical than descriptive. Perhaps a better name would be the “browser object,” because
the navigator object contains lots of information about the browser and the operating system in which
it's running.
Probably the most common use of the navigator object is for handling browser differences. Using its
properties, you can fi nd out which browser, version, and operating system the user has. You can then
Search WWH ::




Custom Search