6

Does anyone have a good solution for getting and setting variables in window.location.hash?

Take a URL that looks like this:

domain.com/#q=1&s=2

What I'd like is an unstressful way - JavaScript or jQuery - to check the values of q and s when the page loads, and change them following events on the page.

I have found some code for getting hash variables, but nothing sensible for setting them.

Am I missing something really obvious, or do I need to roll my own solution (and release it!)?

Thanks.

AP257
  • 89,519
  • 86
  • 202
  • 261

3 Answers3

3

Haven't used it but there is jHash

jHash allows you to work with the 'location.hash' value in a similar fashion to a server-side query string.

This library utilizes the HTML5 "onhashchange" event, but also includes a fall back to still allow the change notifications to work properly in older web browsers.

Stu Thompson
  • 38,370
  • 19
  • 110
  • 156
macarthy
  • 3,074
  • 2
  • 23
  • 24
1

jQuery BBQ can do this.

See also:


Edit as @gonchuki points out, jQuery.query can also do this.

Community
  • 1
  • 1
Matt Ball
  • 354,903
  • 100
  • 647
  • 710
0

JHash didn't work for me in that I wanted it to trigger the routes right away. I personally used routie instead.

It lets you do advanced routing just like jHash but will trigger on page load correctly.

Below will match example.com/#users/john

routie('users/:name', function(name) {
    //name == 'bob';
});
Joseph Astrahan
  • 8,659
  • 12
  • 83
  • 154