A simple snippet to convert a query string to an easy to use object
Go to: About | Setup | Usage | Reference
This script simply converts this...
?name="Tim"&job="coder"&id="123"
...to this...
query.name = "Tim"
query.job = "coder"
query.id = "123"
...allowing the variables to be easily used.
When I first wanted to use variables from a query string, I thought there must be a built in function or method to grab them.
Apparently not, so I wrote this first for my own use, but then thought I'd share it because of how helpful it might be.
var queryString = location.search.substring(1).split('&');
var query = {};
for (var i = 0; i < queryString.length; i++) {
var temp = queryString[i].split('=');
query[temp[0]] = temp[1];
}
Either download or attach the production code found at the URL below
https://thecoolblackcat.github.io/QueryStringHandler/script.min.js
Even though this script creates three variables, you actually only need to reference one object to access values.
See 'Reference' below for the complete list of variables
To Access Values...
query.name // = "value"
query["name"] // = "value"
Where 'name' is any variable you are accessing, and 'value' is the value of that variable
N.B. The value will ALWAYS be returned as a string
The following variables are created by the script
They are listed in the order of their declaration
queryString Stores the full length query string as an array, with values that look like ["name=value"]
query Initially declared blank, it stores the queries as an object, and has the contents of the query string added to it
temp Temporarily stores the values in the loop as an array. For each item, it stores the name at index '0' and the value at index '1'
N.B. To find out more about how the code works, refer to the comments in the 'code.js' file