How to Get Query String Parameters Values in JavaScript
Topic: JavaScript / jQueryPrev|Next
Answer: Use the URL API
You can simply use the URL API to get the parameters values from a query string (also called GET parameters or URL parameters) in JavaScript. The URL API is supported in all major modern browsers.
Let's assume we have a URL with query string or GET parameters as follows:
https://www.example.com/page.html?name=john-clark&age=24
Now let's see how to retrieve the name and age parameters values from the above URL. To get the whole URL you can use the window.location.href
property.
Example
Try this code »// Sample URL
var urlString = "https://www.example.com/page.html?name=john-clark&age=24";
// Creating URL object for the given URL
var url = new URL(urlString);
// Retrieving query string values
var name = url.searchParams.get("name");
console.log(name); // Prints: john-clark
var age = url.searchParams.get("age");
console.log(age); // Prints: 24
Related FAQ
Here are some more FAQ related to this topic: