Skip to content

Instantly share code, notes, and snippets.

@cemerson
Created February 16, 2014 15:51
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 cemerson/9036307 to your computer and use it in GitHub Desktop.
Save cemerson/9036307 to your computer and use it in GitHub Desktop.
JavaScript: Get QueryString URL Parameter value
function getURLParameter(name,usePoundInsteadOfQuestionMarkIfAvailable) {
var url = window.location.href;
// use #param=value method?
if(url.indexOf('?',0) == -1) usePoundInsteadOfQuestionMarkIfAvailable = true;
var poundExistsInURL = (url.indexOf('#',0) > -1);
if(
(usePoundInsteadOfQuestionMarkIfAvailable == true) &&
(poundExistsInURL)){
return getHashParams()[name];
}
// otherwise use ?param=value method
name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
var regexS = "[\\?&]"+name+"=([^&#]*)";
var regex = new RegExp( regexS );
var results = regex.exec( url );
if( results == null )
return "";
else
return results[1];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment