The location ObjectThe location object holds information about the location of the current web page, such as its URL, the domain name , path , server port, and more. This object even enables you to navigate to a new URL just by assigning that URL to the location object's href propertyor, in most browsers, even to the location object itself (like this, window.location = "http://www.w3.org" ). The location object not only holds the current location in the href property, but also lets you dissect that URL in a handy way, using several properties such as hostname (the www. name .com part), host (the same as the hostname property with a port number appended if there is onefor example, in the URL http://www. name .com:80/index.html , the hostname is www. name .com:80 ), href (the current URL in full), hash (the part of the URL following the hash mark, #, in the URL if there is one), and pathname (the file name or path specified in the URL). For example, take a look at this URL that discusses the changes in the HTML 4.01 W3C specification: http://www.w3.org/TR/html4/appendix/changes.html#19991224 Here are the values of various location properties for this URL:
We'll see these properties in more detail in a page or two. You'll find the properties and methods (there are no events) of the location object in Table 10.7. Table 10.7. The Properties and Methods of the location Object
The location object is also a good one when you want to redirect a browser; all you have to do is to assign a new URL to the location.href property. Let's take a look at all the properties and methods of this object now, starting with the properties. |