Created
February 16, 2014 15:51
-
-
Save cemerson/9036307 to your computer and use it in GitHub Desktop.
JavaScript: Get QueryString URL Parameter value
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
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