Skip to content

Instantly share code, notes, and snippets.

@joshcanhelp
Last active February 5, 2017 03:39
Show Gist options
  • Select an option

  • Save joshcanhelp/20ce08247073e1c443b37af343d992f1 to your computer and use it in GitHub Desktop.

Select an option

Save joshcanhelp/20ce08247073e1c443b37af343d992f1 to your computer and use it in GitHub Desktop.
Get the various parameters from a URL piece, hash or query
/**
* Get the various parameters from a URL piece, hash or query
*
* @param params
*
* @returns {{}}
*/
function parseUrlParams( params ) {
'use strict';
// Nothing to parse
if ( params.indexOf( '&' ) < 0 ) {
return {};
}
var pieces = {};
// Normalize param string in case it was passed in with prepended char
params = params.replace( '?', '' );
params = params.replace( '#', '' );
params.split( '&' ).forEach( function ( el ) {
pieces[ el.split( '=' )[ 0 ] ] = el.split( '=' )[ 1 ];
} );
return pieces;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment