JavaScript Tutorial

BOM Location (Window Location)

The window.location object in the Browser Object Model (BOM) provides information about the current page's URL and enables navigation to new pages.

Window Location

The window.location object can be accessed directly without the window prefix. Here are some examples of using window.location:

  • window.location.href: Returns the complete URL (href) of the current page.
  • window.location.hostname: Returns the domain name of the web host.
  • window.location.pathname: Returns the path and filename of the current page.
  • window.location.protocol: Returns the web protocol used (http: or https:).
  • window.location.assign(): Loads a new document by navigating to a new URL.

These properties and methods of the window.location object allow you to retrieve and manipulate information related to the URL of the current page and perform page redirections if needed.

Window Location Href

The window.location.href property is used to retrieve the URL (or href) of the current page. It returns a string that represents the complete URL, including the protocol (e.g., http:// or https://), domain name, path, and any query parameters.

Example :-

Result :-

Your Page location is http://www.lecturely.com/javascript/location.php

Window Location Hostname

The window.location.hostname property is used to get the name of the internet host (of the current page).

Example :-

Result :-

Your Page hostname is www.lecturely.com

Window Location Pathname

The window.location.pathname property is used to retrieve the path and filename of the current page's URL. It returns a string that represents the path component of the URL.

Example :-

Result :-

Your Page path is /javascript/location.php

Window Location Protocol

The window.location.protocol property is used to get the web protocol of the page.

Example :-

Result :-

Your Page protocol is http:

Window Location Port

The window.location.port property is used to retrieve the internet host port number of the current page.

Example :-

Result :-

Your Page port is

Note:- Most browsers will not display default port numbers (80 for http and 443 for https)

Window Location Assign

The window.location.assign() method is used to load a new document in the browser.

Input:-

Output (Before Click):-

Output (After Click will open lecturely.com):-

Go back to Previous Course