Updating a hash programmatically without triggering a hashchange event?

I use the jQuery plugin for BBQ to push states on location.hash.

To prevent a feedback loop, I would like to temporarily disable the hashman listener when setting the state programmatically.

I saw this solution: Change the hash without triggering the hashchange event

Unfortunately, it does not seem absolutely reliable, as it sometimes works, even if I do it:

updateURL(obj){ $(window).unbind( 'hashchange'); $.bbq.pushState(obj); setTimeout( function() { bindHashChange()}, 500); } 

Is there now a more efficient approach to pushing states programmatically? Perhaps another JS library?

+7
source share
1 answer

Doing this with a timeout will not work reliably. An important part of the linked answer is setting a flag that your handler knows about. See Updated Question for Code.

Also, attach a temporary handler to the event that is responsible for restoring your handler:

 function updateState(state, handler) { var win = $(window); function temporaryHandler() { win.unbind('hashchange', temporaryHandler); win.bind('hashchange', handler); }; win.unbind('hashchange', handler); win.bind('hashchange', temporaryHandler); $.bbq.pushState(state); } 
+2
source

All Articles