Last active
November 4, 2018 16:46
-
-
Save AndreasPizsa/03077df29250222784ee to your computer and use it in GitHub Desktop.
Parse URL query string in JavaScript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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