Window Location Object

Window Location Object

The Location object contains information about the current URL.

The Location object is an attribute of the window object.

The Location object can be accessed in the following ways:

window.location or simply location

Instance

let origin = window.location.origin;

Try it yourself

let origin = location.origin;

Try it yourself

Location object properties

Properties Description
hash Set or return the anchor part (#) of the URL.
host Set or return the hostname and port number of the URL.
hostname Set or return the hostname of the URL.
href Set or return the entire URL.
origin Return the protocol, hostname, and port number of the URL.
pathname Set or return the path name of the URL.
port Set or return the port number of the URL.
protocol Set or return the protocol of the URL.
search Set or return the query string part of the URL.

Location object methods

Method Description
assign() Load a new document.
reload() Reload the current document.
replace() Replace the current document with a new document.

Location object description

The Location object is stored in the Location property of the Window object and represents the web address of the document currently displayed in that window. Its href propertyIt stores the complete URL of the document, and other properties describe different parts of the URL. These properties are very similar to the URL properties of the Anchor object (or Area object). When a Location object is converted to a string, the value of the href property is returned. This means you can use the expression location instead of location.href.

However, the Anchor object represents a hyperlink in the document, while the Location object represents the URL (or location) of the document currently displayed in the browser. But the Location object can do much more than that; it can also control the position of the document displayed by the browser. If you assign a string containing a URL to the Location object or its href property, the browser will load and display the document pointed to by the new URL.

In addition to setting location or location.href to replace the current URL with a complete URL, you can also modify part of the URL by assigning values to other properties of the Location object. This will create a new URL with some parts different from the original URL, and the browser will load and display it. For example, if you set the href property of the Location object to a string containing a URL, the browser will load and display the document pointed to by the new URL. hash propertythe browser will jump to a specified location within the current document. Similarly, if set search propertyIf so, the browser will reload the URL with the new query string attached.

In addition to the URL property, the Location Object's reload() MethodCan reload the current document,replace() Can load a new document without creating a new history record for it, that is, in the browser's history list, the new document will replace the current document.