Is there a better way to convert a URL's location.search as an object? Maybe just more efficient or trimmed down? I'm using jQuery, but pure JS can work too.
var query = window.location.search.substring(1), queryPairs = query.split('&'), queryJSON = {};
$.each(queryPairs, function() { queryJSON[this.split('=')[0]] = this.split('=')[1]; });
Just wanted to share this solution using a bit of ESNext and a reducer.
It does pretty much the same suggested by @Carlo but it's a bit cleaner if you're comfortable with ES6 and reducers.
Building on @rafaelbiten's ES6 work, I added support for params that have no value and query param arrays of the duplicate entry style.
JSFiddle: https://jsfiddle.net/w922xefs/
Note --No doubt above solution works, but it wont cover all the operators
Guess you would want something like this-
ex -
Override search @1st line with
Output you get is
Here's a pure JS function. Parses the search part of the current URL and returns an object. (It's a bit verbose for readability, mind.)
On a related note, you're not trying to store the single parameters in "a JSON" but in "an object". ;)
In case someone is looking just to access the search query parameters, use this function:
Easy to use just call
getQueryStringValue(term)
If you are using modern browser this produce the same result as accepted answer: