Last active
December 31, 2015 04:29
-
-
Save smhmic/7934153 to your computer and use it in GitHub Desktop.
get query variable via js
This file contains hidden or 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 getParameterByName( name ){ | |
if( typeof getParameterByName.params == 'undefined' ) | |
getParameterByName.params = {}; | |
if( typeof getParameterByName.params[name] == 'undefined' ){ | |
name = name.replace( /[\[]/, "\\\[" ).replace( /[\]]/, "\\\]" ); | |
var regex = new RegExp( "[\\?&]" + name + "(?:=([^&#]*))?" ), | |
results = regex.exec( location.search ); | |
// if only key was given, assume value is 'true' | |
if( results === null ) | |
getParameterByName.params[name] = null; | |
else if( typeof results[1] == 'undefined' ) | |
getParameterByName.params[name] = true; | |
else | |
getParameterByName.params[name] = decodeURIComponent( results[1].replace( /\+/g, " " ) ); | |
} | |
return getParameterByName.params[name]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment