O'Reilly logo

JavaScript: The Definitive Guide, 6th Edition by David Flanagan

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Browser Location and Navigation

The location property of the Window object refers to a Location object, which represents the current URL of the document displayed in the window, and which also defines methods for making the window load a new document.

The location property of the Document object also refers to the Location object:

window.location === document.location   // always true

The Document object also has a URL property, which is a static string that holds the URL of the document when it was first loaded. If you navigate to fragment identifiers (like “#table-of-contents”) within the document, the Location object is updated to reflect this, but the document.URL property remains unchanged.

Parsing URLs

The location property of a window is a reference to a Location object; it represents the current URL of the document being displayed in that window. The href property of the Location object is a string that contains the complete text of the URL. The toString() method of the Location object returns the value of the href property, so in contexts that will implicitly invoke toString(), you can just write location rather than location.href.

Other properties of this object—protocol, host, hostname, port, pathname, search, and hash—specify the various individual parts of the URL. They are known as “URL decomposition” properties, and they are also supported by Link objects (created by <a> and <area> elements in HTML documents). See the Location and Link entries in Part IV for further details. ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required