Skip to content

Instantly share code, notes, and snippets.

@remcoder
Created April 18, 2012 14:39
Show Gist options
  • Save remcoder/2414015 to your computer and use it in GitHub Desktop.
Save remcoder/2414015 to your computer and use it in GitHub Desktop.
access querystring params from javascript
// adapted from: http://stackoverflow.com/questions/901115/get-query-string-values-in-javascript
// wrapped it in a module and only exposing a getter function make the datastructure immutable
// usage: var bla = QueryString.get('loginEnabled')
var QueryString = (function () {
var urlParams = {};
var e,
a = /\+/g, // Regex for replacing addition symbol with a space
r = /([^&=]+)=?([^&]*)/g,
d = function (s) { return decodeURIComponent(s.replace(a, " ")); },
q = window.location.search.substring(1);
while (e = r.exec(q))
urlParams[d(e[1])] = d(e[2]);
function get(key) {
return urlParams[key];
}
return {
get: get
}
})();
@robflaherty
Copy link

Missing the assignment operator :)
var QueryString = (function () {

@remcoder
Copy link
Author

remcoder commented May 6, 2012

thx! :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment