JavaScript: get and set URL hash parameters?
javascript
Solution
If you want to parse a hash URL:
var hash = window.location.hash.substr(1);
var result = hash.split('&').reduce(function (res, item) {
var parts = item.split('=');
res[parts[0]] = parts[1];
return res;
}, {});
That way, if you have this: `http://example.com/#from=2012-01-05&to=2013-01-01`
It becomes: `{'from': '2012-01-05', 'to':'2013-01-01'}`
As @Dean Stamler notes in the comments, dont forget the empty starting object. `}, {});`
Now to set a hash URL:
`window.location.hash = "from=2012-01-05&to=2013-01-01";`
Problem
How do you get and set URL hash parameters in pure JavaScript? For example, I'd like to use parameters like this: `myurl.com/#from=2012-01-05&to=2013-01-01` And I'd like to be able to get and set the `from` and `to` parameters in the above. I'm happy to use the HTML5 history API if that's the best way of doing things.