Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript query string [closed]

Is there any JavaScript library that makes a dictionary out of the query string, ASP.NET style?

Something which can be used like:

var query = window.location.querystring["query"]? 

Is "query string" called something else outside the .NET realm? Why isn't location.search broken into a key/value collection ?

EDIT: I have written my own function, but does any major JavaScript library do this?

like image 922
core Avatar asked Mar 15 '09 03:03

core


People also ask

How do you parse in Querystring?

The ParseQueryString method uses UTF8 format to parse the query string In the returned NameValueCollection, URL-encoded characters are decoded and multiple occurrences of the same query string parameter are listed as a single entry with a comma separating each value.

What is URLSearchParams?

The URLSearchParams interface defines utility methods to work with the query string of a URL.

How to get query string parameters in JavaScript?

Summary: in this tutorial, you will learn how to use the URLSearchParams to get query string parameters in JavaScript. To get a query string you can access the search property of the location object: To work with the query string, you can use the URLSearchParams object. const urlParams = new URLSearchParams (location.search);

How to get all query strings of url in JavaScript?

Javascript get all query strings. Here to create an anchor element and use a property called search in the element. The search property returns the queryString completely. After creating an anchor element and assigning the url to href, search returns the query strings of url.

How to get QUERY STRING values using pure JavaScript?

Watch a video course JavaScript - The Complete Guide (Beginner + Advanced) It is possible to get query string values by using pure JavaScript or jQuery. To implement that, create a function, such as getQueryParams and add the code given below: The function takes params and url as a parameters.

How to work with the query string in SQL?

To work with the query string, you can use the URLSearchParams object. const urlParams = new URLSearchParams (location.search); The URLSearchParams is an iterable object, therefore you can use the for...of structure to iterate over its elements which are query string parameters:


2 Answers

You can extract the key/value pairs from the location.search property, this property has the part of the URL that follows the ? symbol, including the ? symbol.

function getQueryString() {   var result = {}, queryString = location.search.slice(1),       re = /([^&=]+)=([^&]*)/g, m;    while (m = re.exec(queryString)) {     result[decodeURIComponent(m[1])] = decodeURIComponent(m[2]);   }    return result; }  // ... var myParam = getQueryString()["myParam"]; 
like image 168
Christian C. Salvadó Avatar answered Oct 14 '22 11:10

Christian C. Salvadó


tl;dr solution on a single(ish) line of code using vanilla javascript

var queryDict = {} location.search.substr(1).split("&").forEach(function(item) {     queryDict[item.split("=")[0]] = item.split("=")[1] }) 

For querystring ?a=1&b=2&c=3&d&eit returns:

> queryDict a: "1" b: "2" c: "3" d: undefined e: undefined 

multi-valued keys and encoded characters?

See the original answer at How can I get query string values in JavaScript?

"?a=1&b=2&c=3&d&e&a=5&a=t%20e%20x%20t&e=http%3A%2F%2Fw3schools.com%2Fmy%20test.asp%3Fname%3Dståle%26car%3Dsaab" > queryDict a: ["1", "5", "t e x t"] b: ["2"] c: ["3"] d: [undefined] e: [undefined, "http://w3schools.com/my test.asp?name=ståle&car=saab"] 
like image 25
Qwerty Avatar answered Oct 14 '22 10:10

Qwerty