Skip to content
Tutorials Center
My learning

Topics

JavaScript Reference ยท Browser and Web APIs

The location object

Reference for window.location: read href, origin, pathname, search and hash, navigate with assign(), replace() and reload(), and parse query strings safely.

window.location (also available as document.location, or just location) represents the URL of the current page. Reading its properties tells you where the page is. Writing to it, or calling its methods, navigates the browser to a new address.

Its properties split the URL into parts, which are the same parts that the URL object gives you. Changing only the hash scrolls to a fragment without reloading; changing anything else loads a new document.

JavaScript
// https://shop.example.com:8443/products/shoes?color=red&size=42#reviews
location.origin;    // "https://shop.example.com:8443"
location.pathname;  // "/products/shoes"
location.search;    // "?color=red&size=42"
location.hash;      // "#reviews"

Try it

The parts of a URL (same names as on location)
Read the query string and change the hash

Syntax

JavaScript
location.href = "https://example.com/";   // navigate (adds a history entry)
location.assign(url);                     // same as setting href
location.replace(url);                    // navigate without a history entry
location.reload();                        // reload the current page
String(location);                         // the full URL (toString())

Properties

Name Description
href The complete URL. Setting it navigates.
origin Scheme, host and port, such as https://example.com. Read-only.
protocol The scheme with a colon: "https:".
host Hostname plus port (the port is omitted when it is the default).
hostname The host without the port.
port The port as a string, or "" for the default.
pathname The path, starting with /.
search The query string including ?, or "".
hash The fragment including #, or "".
ancestorOrigins List of the origins of parent frames (not in every browser).

Methods

Method Description
assign(url) Loads url; the user can go Back.
replace(url) Loads url and replaces the current history entry; Back skips this page.
reload() Reloads the page.
toString() Returns href.

Assigning an invalid URL throws a SyntaxError. Relative URLs are resolved against the current page.

Usage notes

  • assign() versus replace(). Use replace() for redirects (a login page that forwards to the dashboard) so the Back button does not return to the redirecting page.
  • Setting hash does not reload. It adds a history entry, scrolls to a matching element id, and fires hashchange. Setting pathname or search does reload.
  • Parse queries with URLSearchParams. new URLSearchParams(location.search).get("q") handles decoding and repeated keys. Do not split on & by hand.
  • Change URLs with URL. Build a new address with new URL(location.href), edit searchParams, then pass url.href to assign() or history.replaceState().
  • Open redirects. Never send the user to a URL taken from the query string without checking it, such as location.href = params.get("next"). Check that it is a same-origin path first.
  • Do not put user input in href. A javascript: URL assigned to location.href runs code. Validate the scheme.
  • Cross-origin frames. Reading another origin's location throws SecurityError; writing href is allowed for navigation only.
  • Prefer explicit forms. window.location = url works like setting href, but location.href = url or location.assign(url) is clearer.

Browser support

location and its properties and methods work in every browser. ancestorOrigins is not available in all browsers.