Q:

How to Get Query String Parameters Values in JavaScript

0

How to Get Query String Parameters Values in JavaScript

All Answers

need an explanation for this answer? contact us directly to get an explanation for this 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.

// 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

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

JavaScript / jQuery Frequently Asked Questions

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
How to Remove Empty Elements from an Array in Java... >>
<< How to Empty an Array in JavaScript...