Skip to content

Instantly share code, notes, and snippets.

@AndreasPizsa
Last active November 4, 2018 16:46
Show Gist options
  • Save AndreasPizsa/03077df29250222784ee to your computer and use it in GitHub Desktop.
Save AndreasPizsa/03077df29250222784ee to your computer and use it in GitHub Desktop.
Parse URL query string in JavaScript
/**
* sets window.location.query, which is a hash with all parameters
* param names are all lowercase
* reference as window.location.query.myparameter
*/
(function parseWindowLocationQuery(w){
var d=decodeURIComponent,
q=w.location.query=w.location.query||{},
pairs=w.location.search.substr(1).split('&'),
i;
while(i=pairs.pop()) {
var keyValue=i.match(/([^=]*)=?(.*)/)
if(keyValue) q[d(keyValue[1]).toLowerCase()]=d(keyValue[2]);
}
})(window);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment