Skip to content

Instantly share code, notes, and snippets.

@Darep
Created February 12, 2013 12:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Darep/4761978 to your computer and use it in GitHub Desktop.
Save Darep/4761978 to your computer and use it in GitHub Desktop.
<!doctype html>
<html>
<head>
<title></title>
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script src="jquery.parseParams.js"></script>
</head>
<body>
<script>
var hash = '#f=&from=ahvenpolku,+lemp%C3%A4%C3%A4l%C3%A4%C3%A4&to=satakunnankatu,+tampere&via[]=hervanta&via[]=hallila';
var query = hash.split('#')[1] || '';
var params = $.parseParams(query);
console.log( params );
</script>
</body>
</html>
/**
* $.parseParams - parse query string paramaters into an object.
*/
(function($) {
var re = /([^&=]+)=?([^&]*)/g;
var decodeRE = /\+/g; // Regex for replacing addition symbol with a space
var decode = function (str) {return decodeURIComponent( str.replace(decodeRE, " ") );};
$.parseParams = function(query) {
var params = {}, e;
while ( e = re.exec(query) ) {
var k = decode( e[1] ), v = decode( e[2] );
if (k.substring(k.length - 2) === '[]') {
k = k.substring(0, k.length - 2);
(params[k] || (params[k] = [])).push(v);
}
else params[k] = v;
}
return params;
};
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment