The Location Object
By Stephen Bucaro
When you load a Web page into your browser, the path or URL to that page is
stored in the window.location object. You can retrieve the page's URL
from the location object, or you can load a different page into the
window by using the location object's assign or replace methods.
Location Object Properties
href | The URL of the current page |
protocol | The URL's protocol, usually http: |
host | The hostname and port number |
hostname | The name of the host |
pathname | The path relative to the current directory |
port | The port number (default http port is 80) |
hash | An inpage anchor (#) attached to URL |
search | A query string (?) attached to URL |
Location Object Methods
reload() | Reloads the current page |
replace("URL") | Loads the specified URL and disables the Back button |
assign("URL") | Loads the specified URL, does not disable the Back button |
The example code below shows the location object's replace method
being executed by the document's onload event. This causes the webpage to
be replaced by the page at the specified URL immediately after it completes loading.
<body onload="window.location.replace('page2.htm')">
Note the same action can be performed without Java Script using a refresh
meta tag with its content attribute set to 0, as shown below.
<meta http-equiv="refresh" content="0;url=page2.htm">
More Java Script Code: • JavaScript to Concatenate Strings • The continue Statement • Use moveBy Method to Move Window by a Specific Offset • The if/else Structure • JavaScript .big and .small to Change Text Size • Determine Absolute Value • Java Script Number Object • Java Script Include a Backslash Character in a String • Define Lines in a String • Compare Two Strings
|