Section 25.201. Location: represents and controls browser location


25.201. Location: represents and controls browser location

JavaScript 1.0: Object Location

25.201.1. Synopsis

 location window.location 

25.201.2. Properties

The properties of a Location object refer to the various portions of the current document's URL. In each of the following property descriptions, the example given is a portion of this (fictitious) URL:

 http://www.oreilly.com:1234/catalog/search.html?q=JavaScript&m=10#results 


hash

A read/write string property that specifies the anchor portion of the URL, including the leading hash (#) markfor example, "#results". This portion of the document URL specifies the name of an anchor within the document.


host

A read/write string property that specifies the hostname and port portions of the URLfor example, "www.oreilly.com:1234".


hostname

A read/write string property that specifies the hostname portion of a URLfor example, "www.oreilly.com".


href

A read/write string property that specifies the complete text of the document's URL, unlike other Location properties that specify only portions of the URL. Setting this property to a new URL causes the browser to read and display the contents of the new URL.


pathname

A read/write string property that specifies the pathname portion of a URLfor example, "/catalog/search.html".


port

A read/write string (not a number) property that specifies the port portion of a URL for example, "1234".


protocol

A read/write string property that specifies the protocol portion of a URL, including the trailing colonfor example, "http:".


search

A read/write string property that specifies the query portion of a URL, including the leading question markfor example, "?q=JavaScript&m=10".

25.201.3. Methods


reload( )

Reloads the current document from the cache or the server.


replace( )

Replaces the current document with a new one without generating a new entry in the browser's session history.

25.201.4. Description

The location property of the Window object refers to a Location object that represents the web address (the "location") of the document currently displayed in that window. The HRef property contains the complete URL of that document, and the other properties of the Location object each describe a portion of that URL. These properties are much like the URL properties of the Link object. When a Location object is converted to a string, the value of the href property is returned. This means that you can use the expression location in place of location.href.

While the Link object represents a hyperlink in a document, the Location object represents the URL, or location, currently displayed by the browser. However, the Location object does more than that: it also controls the location displayed by the browser. If you assign a string containing a URL to the Location object or to its HRef property, the web browser responds by loading the newly specified URL and displaying the document it refers to.

Instead of setting location or location.href to replace the current URL with a completely new one, you can modify just a portion of the current URL by assigning strings to the other properties of the Location object. This creates a new URL with one new portion, which the browser loads and displays. For example, if you set the hash property of the Location object, you can cause the browser to move to a named location within the current document. Similarly, if you set the search property, you can cause the browser to reload the current URL with a new query string appended.

In addition to its URL properties, the Location object also defines two methods. The reload( ) method reloads the current document. The replace( ) method loads a new document without creating a new history entry for it; the new document replaces the current one in the browser's history list.

25.201.5. See Also

Link, the URL property of the HTMLDocument object




JavaScript. The Definitive Guide
JavaScript: The Definitive Guide
ISBN: 0596101996
EAN: 2147483647
Year: 2004
Pages: 767

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