SKU // 126

JS Location

The location object holds the address of the current page and can send the browser somewhere else. An address is made of named parts, and the same parts show up when you work with any URL in code. This lesson names the parts, reads values out of the query string, builds addresses safely, and covers the ways to navigate.

// The Parts of an Address

Take this address:

https://www.example.com:8080/lessons/page.html?topic=json&level=2#intro

location breaks the current page's address into properties. The built-in URL object has exactly the same properties for any address you give it, which makes it a handy way to practice:

const url = new URL("https://www.example.com:8080/lessons/page.html?topic=json&level=2#intro");

console.log(url.href);
console.log(url.protocol);
console.log(url.host);
console.log(url.hostname);
console.log(url.port);
console.log(url.origin);
console.log(url.pathname);
console.log(url.search);
console.log(url.hash);
https://www.example.com:8080/lessons/page.html?topic=json&level=2#intro https: www.example.com:8080 www.example.com 8080 https://www.example.com:8080 /lessons/page.html ?topic=json&level=2 #intro
Property Holds
hrefthe whole address
protocolhow to talk to the server, with the colon: "https:"
hostthe server name plus the port, if there is one
hostnamethe server name alone
portthe port number as text (empty for the default)
originprotocol + host: who the page belongs to
pathnamethe path to the page, starting with /
searchthe query string, starting with ?
hashthe fragment, starting with #

For your current page, location.pathname is where you are on the site, and location.origin is the site itself.

// Reading the Query String

The query string carries small pieces of information in the address, as name=value pairs joined by &. Do not split it by hand: URLSearchParams reads it correctly, including special characters.

const params = new URLSearchParams("?topic=json&level=2&tag=a&tag=b");

console.log(params.get("topic"));
console.log(params.get("level"), typeof params.get("level"));
console.log(params.get("missing"));
console.log(params.has("level"));
console.log(params.getAll("tag"));
json 2 string null true ["a", "b"]

On a real page you pass it the current query string: new URLSearchParams(location.search). The values arrive as strings, so convert with Number(...) when you need a number.

const params = new URLSearchParams("topic=json&level=2");
for (const [key, value] of params) {
  console.log(key + " = " + value);
}
topic = json level = 2

// Building Addresses Safely

If you glue a query string together with +, a space or an & in the value breaks the address. Let URLSearchParams encode it for you:

const params = new URLSearchParams();
params.set("q", "cats & dogs");
params.set("page", 2);

console.log(params.toString());
console.log("https://example.com/search?" + params);
q=cats+%26+dogs&page=2 https://example.com/search?q=cats+%26+dogs&page=2

// Going Somewhere Else

Assigning to location.href sends the browser to a new address. There are four related ways to move:

Code Does
location.href = "/page.html"go there (adds a history entry, like clicking a link)
location.assign("/page.html")the same, written as a method
location.replace("/page.html")go there without a history entry: Back skips the current page
location.reload()load the current page again
// send the visitor to the home page
location.href = "/";

// like a redirect: the old page is not left in the Back list
location.replace("/login.html");

These are not shown with a Try It button because they would leave the page. Use them on a page of your own.

Do not reload in a loop
A location.reload() that runs every time the page loads reloads forever. Always put navigation behind a condition or a click.

// The Hash

Changing only location.hash does not reload the page. It scrolls to the element with that id and adds a history entry, which is how in-page links work. The "hashchange" event tells you when it changes:

location.hash = "#intro";   // scroll to the element with id="intro"

window.addEventListener("hashchange", function () {
  console.log("now at", location.hash);
});
Treat the address as untrusted
Anyone can craft a link with anything in its query string or hash. If you show a value from the address on the page, use textContent (not innerHTML), or someone can build a link that injects markup into your page.

// Quick Check

Which property gives "?topic=json&level=2" for the current page?

  1. location.hash
  2. location.search
  3. location.pathname
  4. location.host
Show answer
B. search is the query string, starting with ?. hash starts with #, and pathname is the path.

What does this log?

const p = new URLSearchParams("n=5");
console.log(typeof p.get("n"));
Show answer
string. Values read from a query string are always text. Use Number(p.get("n")) to get a number.

What is the difference between location.assign(url) and location.replace(url)?

Show answer
assign adds a history entry, so the Back button returns to the current page. replace does not, so Back skips it.

// Try It

  1. Click Open Playground and run it. Change the address and run it again to see which parts change
  2. Add a port to the address and a second tag value to the query, then read them
  3. Build a search address for the text rock & roll with URLSearchParams
  4. Write the line that sends a visitor to /home.html without leaving the current page in the Back list
// Video Walkthroughs
Last updated: Oct 11, 2026, 6:47 PM ET