Skip to content

Instantly share code, notes, and snippets.

@jmahc
Created May 29, 2015 13:06
Show Gist options
  • Save jmahc/d21e90f027c86f5c7943 to your computer and use it in GitHub Desktop.
Save jmahc/d21e90f027c86f5c7943 to your computer and use it in GitHub Desktop.
This javascript function grabs any query parameters and stores them in an array
var getQueryParams = function () {
var params = [];
var query = window.location.search;
query = query.slice(1, query.length);
var nv = query.split('&');
for (var i = 0; i < nv.length; i++) {
var q = nv[i].split('=');
params[q[0]] = q[1];
}
return params;
};
//-- Example --
//www.example.com?product=1234&type=Shirt
var parameters = getQueryParams(); //Calls the function and stores values into variable 'parameters'
var productNumber = parameters.product; // 1234
var productType = parameters.type; // Shirt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment