| JavaScript - Browser navigation | | Print | |
Browser navigation
The location property of the Window object refers to the contents of the browser's location bar (the field that you type URLs into). Reading the value of this property gives you the URL that is currently being displayed. More importantly, setting the location property to a new URL tells the browser to load and display the document referred to by that URL:
// In old browsers, load a different page
if (parseInt(navigator.appVersion) <= 4)
location = "staticpage.html";
Note that any script or event handler that sets the location property of its own window (we'll discuss multiple windows and multiple frames later in this section) is overwritten when the new document is loaded and will not continue running!
Although the location property can be queried and set as if it were a string, it actually refers to a Location object. The Location object has properties that allow you to query and set individual portions of the currently displayed URL:
// Get the substring of the URL following ?
var query = location.search.substring(1);
// Scroll to a named portion of the document
location.hash = "#top";
In addition, the reload( ) method makes the browser reload the currently displayed URL.
The history property of the Window object refers to the History object for the browser window. This object defines methods that allow you to move the browser backward and forward through its browsing history, just as the user can with the browser's Back and Forward buttons:
history.back(); // Go back once
history.forward(); // Go forward
history.go(-3); // Go back three times
| Users' Comments (0) |
|
No comment posted





