Created
January 26, 2011 13:04
-
-
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
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
/** | |
* 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