Skip to content

Instantly share code, notes, and snippets.

@smhmic
Last active December 31, 2015 04:29
Show Gist options
  • Save smhmic/7934153 to your computer and use it in GitHub Desktop.
Save smhmic/7934153 to your computer and use it in GitHub Desktop.
get query variable via js
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