Is it for hash or for redirection? What are you trying to do? This kind of action is usually very intrusive.
You can try "onbeforeunload" for this javascript before leaving the page
Edited
Actually, the link you provide is accurate enough.
var hash = location.hash; setInterval(function() { if (location.hash != hash) { hashUpdatedEvent(hash); } }, 100); function hashUpdatedEvent(hash) { switch(...); }
Your duplicate link problem will be fixed if you change
<a href="javascript:void(0)" onclick="someFuncion()">Go for it</a> function someFuncion() { doWhatever(); location.hash = 'somethingwasdone'; } function hashUpdatedEvent(hash) { if(hash == 'somethingwasdone') { doWhatever(); } }
Just (update the hash and let the "event" handle the action):
<a href="javascript:void(0)" onclick="someFuncion()">Go for it</a> function someFuncion() { location.hash = 'somethingwasdone'; } function hashUpdatedEvent(hash) { if(hash == 'somethingwasdone') { doWhatever(); } }
source share