Skip to content

Instantly share code, notes, and snippets.

@mozz100
Created February 1, 2012 15:19
Show Gist options
  • Save mozz100/1717497 to your computer and use it in GitHub Desktop.
Save mozz100/1717497 to your computer and use it in GitHub Desktop.
Read the querystring from javascript
// Adapted from http://stackoverflow.com/questions/901115/get-query-string-values-in-javascript
// Give a querystring like this: yourpage.html?cheese=wensleydale&biscuits=nice
var qs = (function(a) {
if (a == "") return {};
var b = {};
for (var i = 0; i < a.length; ++i)
{
var p=a[i].split('=');
if (p.length != 2) continue;
b[p[0]] = decodeURIComponent(p[1].replace(/\+/g, " "));
}
return b;
})(window.location.search.substr(1).split('&'));
console.log(qs['cheese']); // outputs 'wensleydale'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment