How to Get the Query String from the URL in JavaScript with location.search
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
window.location.search
| Part | What it does |
|---|---|
window.location | The address of the page the script runs in. location on its own means the same thing. |
.search | The 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:

<!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:

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 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.searchwas?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
| Mistake | What happens | Do this instead |
|---|---|---|
Expecting the # part in search | For ?name=Ana#top, search was ?name=Ana, and #top was in window.location.hash | Read window.location.hash for the part after # |
| Expecting decoded text | For ?city=New%20York, search was still ?city=New%20York | Read values with URLSearchParams, which gave New York |
| Cutting a value out of the string by hand | Code that splits on & and = breaks on encoded characters and repeated names | Use new URLSearchParams(window.location.search) |
Forgetting that search starts with ? | A check like search === 'name=Ana' is never true | Compare with the ?, or read the value with URLSearchParams |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
| Nothing appears after "The query string is:" | The address has no ? part | Add ?name=Ana to the end of the address and press Enter |
The text shows %20 instead of a space | search is the raw text of the address | Read the value with URLSearchParams |
| The page doesn't change after you edit the address | You didn't press Enter, so the page didn't load again | Press 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
- Location: search property, on MDN.
- Window: location property, on MDN.
- URLSearchParams, on MDN.
