Javascript/jQuery detect hash change only on browser back/forward button click

Chris picture Chris · Nov 10, 2011 · Viewed 14.3k times · Source

Is it possible to detect the hashchange only on a browser history change (i.e. Back or Forward button)?

I have seen the onBeforeUnload event, but this event does not fire on hash change, as the window is not unloading.

The hashchange event obviously fires anytime the hash changes. Any fix? Preferably without a plugin. I have seen the jQuery history plugin, but am looking for the simplest solution.

Thanks for the help.

Answer

frattaro picture frattaro · Apr 16, 2013

I ended up marking a flag whenever my navigation was triggered, and if the flag was marked, the hashChange event would ignore it, otherwise it would handle it as if it was a back/forward event.