How to get query string value in jquery?

by madie.koelpin , in category: JavaScript , 2 years ago

How to get query string value in jquery?

Facebook Twitter LinkedIn Telegram Whatsapp

2 answers

by dmitrypro77 , 2 years ago

@madie.koelpin You can use vanilla Javascript and URLSearchParams() to parse and get any string value, here is code as example:


1
2
3
4
5
6
let url = new URL('http://google.com/search?query=test&limit=10');
let params = new URLSearchParams(url.search);
// Output: test
console.log(params.get('query'))
// Output: 10
console.log(params.get('limit'))
by moriah.medhurst , a year ago

@madie.koelpin 

You can use the URLSearchParams object to get query string values in jQuery. Here's an example:


Suppose you have the following URL: https://example.com/?name=John&age=30


To get the value of the name parameter in jQuery, you can use the following code:

1
2
var queryString = new URLSearchParams(window.location.search);
var name = queryString.get('name');


This code creates a new URLSearchParams object with the query string of the current URL (window.location.search). Then, the get method is used to retrieve the value of the name parameter.


You can modify the code to get the value of any other query string parameter by changing the parameter name in the get method.