Skip to content

Instantly share code, notes, and snippets.

@quinns
Last active June 20, 2018 22:47
Show Gist options
  • Save quinns/e889a66c3cbdca6daa358c5a5d2b9407 to your computer and use it in GitHub Desktop.
Save quinns/e889a66c3cbdca6daa358c5a5d2b9407 to your computer and use it in GitHub Desktop.
Get URL parameters in JavaScript
/**
* JavaScript Get URL Parameter via https://www.kevinleary.net/javascript-get-url-parameters/
*
* @param String prop The specific URL parameter you want to retreive the value for
* @return String|Object If prop is provided a string value is returned, otherwise an object of all properties is returned
*/
function getUrlParams( prop ) {
var params = {};
var search = decodeURIComponent( window.location.href.slice( window.location.href.indexOf( '?' ) + 1 ) );
var definitions = search.split( '&' );
definitions.forEach( function( val, key ) {
var parts = val.split( '=', 2 );
params[ parts[ 0 ] ] = parts[ 1 ];
} );
return ( prop && prop in params ) ? params[ prop ] : params;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment