Skip to content

Instantly share code, notes, and snippets.

@geraldfullam
Created January 7, 2015 04:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save geraldfullam/6bf5cd6305a43747f880 to your computer and use it in GitHub Desktop.
Save geraldfullam/6bf5cd6305a43747f880 to your computer and use it in GitHub Desktop.
Get query parameter values by key name from window.location.search string
/* Based on: http://stackoverflow.com/a/901144/2502532 */
var getParam = function(key) {
key = key.replace(/[\[]/, '\\[').replace(/[\]]/, '\\]');
var val = window.location.search.match(new RegExp('[\\?&]' + key + '=([^&#]*)'));
return val === null ? '' : decodeURIComponent(val[1].replace(/\+/g, ' '));
};
console.log(getParam('foo'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment