Created
April 18, 2012 14:39
-
-
Save remcoder/2414015 to your computer and use it in GitHub Desktop.
access querystring params from javascript
This file contains 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
// 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 | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
thx! :-)