The location Object


The location Object

The 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:

  • hash : #19991224

  • host : www.w3.org

  • hostname : www.w3.org

  • href : http://www.w3.org/TR/html4/appendix/changes.html#19991224

  • pathname : /TR/html4/appendix/changes.html

  • protocol : http:

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

Properties

Methods

hash

assign

host

reload

hostname

replace

href

 

pathname

 

port

 

protocol

 

search

 

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.



Inside Javascript
Inside JavaScript
ISBN: 0735712859
EAN: 2147483647
Year: 2005
Pages: 492
Authors: Steve Holzner

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net