Lesson

JS Location

10 min reading
Free Course

JavaScript Location: Read URLs, Query Params, and Redirects

The JavaScript location object window. locationtells your code where the browser currently is and lets you send it somewhere else. You can use it to read the current address, pull out a search term, or redirect a user after login. Combined with the URL and URLSearchParams classes, it replaces most of the fragile string-splitting code developers used to write by hand.

This guide covers how the URL is broken into parts, how to read and change query parameters, how to redirect without breaking Back-button behavior, and which mistakes to avoid.

What Is window.location in JavaScript?

window.location is a built-in browser object that represents the URL of the current page. You can read from it to find out where the user is, and write to it to navigate. Because it belongs to the global window object, you can write either window. location or just location.

Anatomy of a URL in window.location

For a URL like https://kodersolution.com:443/tutorials/javascript?page=2#faq:

  • location.origin: "https://kodersolution.com:443" (scheme + host + port)

  • location.pathname: "/tutorials/javascript"

  • location.search: "?page=2" (query string)

  • location.hash: "#faq" (fragment identifier)

Parsing Query Parameters and Redirecting Safely

Use new URLSearchParams(location.search) to read query parameters cleanly, and choose between location.assign(url) (keeps Back button history) and location.replace(url) (replaces current history entry).

console.log("Current href:", window.location.href);
console.log("Pathname:", window.location.pathname);

// Parse query parameters reliably using URL and URLSearchParams
const sampleUrl = new URL("https://kodersolution.com/search?q=async+await&page=2");
const params = sampleUrl.searchParams;

console.log("Search term:", params.get("q"));     // "async await"
console.log("Page number:", params.get("page"));  // "2"

// Build safe redirect URLs without string-concatenation bugs
params.set("page", "3");
console.log("Next URL:", sampleUrl.toString());

Common Mistakes to Avoid

  • Redirecting directly to unvalidated ?next= query parameters: Passing user-supplied URLs straight to location.href = nextUrl creates Open Redirect phishing vulnerabilities. Validate that relative paths start with / and are not //evil.com.

  • Splitting location.search manually with .split("&"): Manual splitting breaks on + spaces and %20 URL encoding. Always use URLSearchParams.

  • Calling location.reload(true) expecting cache bypass: Modern browsers ignore the deprecated boolean parameter on location.reload().

How to Check That Your Code Works

  • Open the browser DevTools console on any page and run console.log(location.href) to confirm what you're working with.

  • Test your query parsing by visiting the page with different parameters, including missing ones and values containing spaces or &.

  • After a redirect, press the Back button to confirm the history behaves as intended (assign vs replace).

  • For redirect validation, try inputs like https://evil.example, //evil.example, and /dashboard to confirm only the last one is allowed.

Frequently Asked Questions

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

location.assign(url) (or setting location.href = url) navigates while keeping the current page in the browser's Back history. location.replace(url) overwrites the current history entry, so pressing Back skips the old page—useful after login redirects.

How do I update query parameters without reloading the page?

Build the updated URL with new URL(window.location.href) and update the browser URL field silently using history.replaceState(null, "", url).

Does changing location.hash reload the page?

No, changing #section scrolls to that matching element ID (if present) and fires a "hashchange" event without making a new server request.

Save Your Progress

Unlock Your
Full Potential.

Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.

Quick Access With

Enterprise-Grade Security Protocol

Try it Yourself

Experiment with the code from this lesson in our interactive playground.

Open Playground
Lesson Recap Quiz Available

Test Your Knowledge

You've completed this section! Take a quick 5-question quiz to check your understanding.

JavaScript Topic Hub

Stuck on this lesson?

Join our community of senior developers.

Ask in Forum