views:

49

answers:

1

I'm making an ajax site. How would I go about executing some javascript every time the url changes. Say when "/#page/aboutus/" changes to "/#page/news/".

I want users to be able to bookmark ajax-pages.

+4  A: 

This may be easier to do a different way, for example a .live() event handler:

$("a").live('click', function() {
  //do something with this.hash
});

But, if you do indeed need to trigger with location.hash changes, you can use the onhashchange plugin for this (at least until it's supported natively in every browser).

For example:

$(window).bind('hashchange', function(){
  //do something with location.hash here
});
Nick Craver
A check for window.location.hash on page load and one event handler should take care of my problem. Thanks.