Skip to content

Instantly share code, notes, and snippets.

@Krule
Created January 26, 2011 13:04
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Krule/796655 to your computer and use it in GitHub Desktop.
Save Krule/796655 to your computer and use it in GitHub Desktop.
Small javascript snippet to convert location.search parameters and return them as hash
/**
* Returns location search parameters as hash, so we can
* use them more naturally in our javascript. Like...cough... rails :P
*
* Example url: http://my_domain.ext/?page=products&condition=cheap
*
*
* var params = get_params();
*
* alert( params['page'] ); // => products
*
* alert( params['condition'] ); // => cheap
*
*/
var get_params = function(){
var params = location.search.substr(1).split("&");
var obj = {}
for (var i=0; i < params.length; i++) {
obj[params[i].split("=")[0]] = params[i].split("=")[1]
}
return obj;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment