Window Location


The window.location object provides information about the current URL or the URL of the current web page. It allows you to read and modify various properties related to the browser's location.

Here are some commonly used properties of the window.location object:

location.href

Returns the complete URL of the current page.

Example

Preview

location.protocol

Returns the protocol of the current URL, such as "http:" or "https:".

Example

Preview

location.host

Returns the hostname and port number of the current URL.

Example

Preview

location.hostname

Returns the hostname (domain) of the current URL.

Example

Preview

location.port

Returns the port number of the current URL, if specified.

Example

Preview

location.pathname

Returns the path and filename of the current URL.

Example

Preview

location.search

Returns the query string part of the current URL.

Example

Preview

location.hash

Returns the fragment identifier (anchor) part of the current URL.

By accessing these properties, you can obtain various details about the current URL, such as its protocol, hostname, path, query parameters, and fragment identifier. This information can be used to perform URL manipulations, retrieve specific parts of the URL, or navigate to different pages programmatically.

Example

Preview

assign()

The assign() method of window.location is used to load a new document. It navigates the browser to the specified URL.

Example

Preview

reload()

The reload() method of window.location is used to reload the current web page. It reloads the page with the same URL.

Example

Preview

replace()

The replace() method of window.location is used to replace the current document with a new one. It navigates the browser to the specified URL, but it does not store the current page in the session history.

Example

Preview

location.origin

The origin property of window.location represents the protocol, hostname, and port number of the current URL. It provides the base URL of the current page.

Example

Preview