Skip to main content

How to Get the Query String from the URL in JavaScript with location.search

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

window.location.search is the query string of the page's address: the part that starts with ?, such as ?name=Ana&city=Paris. It's an empty string when the address has no query string, and it never includes the # part.

Syntax​

Syntax
window.location.search
PartWhat it does
window.locationThe address of the page the script runs in. location on its own means the same thing.
.searchThe query string: the part of the address from the ? up to the #, or to the end when there's no #. When the address has no ?, it's '', an empty string.

MDN's Location: search property page describes it the same way, the leading ? included.

The Example​

The example is a page that shows its own query string in bold. You open it twice: first as it is, then with ?name=Ana&city=Paris added to the end of its address.

Step 1: Write the Code​

Create a file named index.html in a text editor, such as Visual Studio Code:

index.html, a page with a paragraph that says The query string is, followed by an empty strong element with the id query, and a script that sets that element's textContent to window.location.search.

index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Query string</title>
</head>
<body>
<p>The query string is: <strong id="query"></strong></p>

<script>
document.getElementById('query').textContent = window.location.search;
</script>
</body>
</html>

document.getElementById('query') finds the <strong> element by its id, and setting its textContent puts the query string inside it, so you can see it on the page.

Step 2: Open It in a Browser​

Double-click index.html. It opens in your browser:

The page in Chromium, showing The query string is, with nothing after it.

Nothing appears after the colon. The address ends in index.html, with no ? part, so window.location.search is an empty string.

Step 3: Add a Query String to the Address​

Click the address bar, add ?name=Ana&city=Paris to the very end of the address, and press Enter:

The page in Chromium after the reload, showing The query string is: ?name=Ana&city=Paris in bold.

The screenshots come from Chromium 159, the open-source browser that Chrome is built on.

What Happened​

  • The page loaded again, and the script ran again. This time the address ended in index.html?name=Ana&city=Paris.
  • window.location.search was ?name=Ana&city=Paris, the ? included.
  • It worked on a page opened straight from a file. The address started with file:///, and the query string was read just as it would be on a website.

Common Mistakes​

MistakeWhat happensDo this instead
Expecting the # part in searchFor ?name=Ana#top, search was ?name=Ana, and #top was in window.location.hashRead window.location.hash for the part after #
Expecting decoded textFor ?city=New%20York, search was still ?city=New%20YorkRead values with URLSearchParams, which gave New York
Cutting a value out of the string by handCode that splits on & and = breaks on encoded characters and repeated namesUse new URLSearchParams(window.location.search)
Forgetting that search starts with ?A check like search === 'name=Ana' is never trueCompare with the ?, or read the value with URLSearchParams

Troubleshooting​

SymptomLikely causeFix
Nothing appears after "The query string is:"The address has no ? partAdd ?name=Ana to the end of the address and press Enter
The text shows %20 instead of a spacesearch is the raw text of the addressRead the value with URLSearchParams
The page doesn't change after you edit the addressYou didn't press Enter, so the page didn't load againPress Enter in the address bar

FAQ​

How do I read one value from the query string?​

Pass search to URLSearchParams and ask for the name: on this page, new URLSearchParams(window.location.search).get('name') returned Ana.

What's the difference between location.search and location.href?​

location.href is the whole address, file:///…/index.html?name=Ana&city=Paris here, while location.search is only the ?name=Ana&city=Paris part.

Is window.location the same as location?​

Yes. In a browser, window.location === location is true, because everything on window can also be written without it.

Conclusion​

window.location.search hands you the page's query string, ? included, or '' when there isn't one. Read single values from it with URLSearchParams, and look in window.location.hash for anything after a #.

Additional Resources​