I don't know if this helps in your situation, but Sammy.js, a popular hash routing library, has a before handler. I used it in my application to write a previously available hash, and if it is a hash, I want them not to go, returns false, will keep them on this page. You still need to rewrite the URL to display the previous page, but it seems to work. Here is a small example of what I did:
app.before(function (context) { if (context.path === lastRoute) return false; // Was fired from our app.setLocation below. if (lastRoute === "/#MyHashWhereIFirstWantToConfirm") { if (!confirm("Are you sure you wish to leave this page?")) { app.setLocation(lastRoute); // Send them back to /#MyHashWhereIFirstWantToConfirm return false; // Keep the handler for the destination page from firing. } } lastRoute = context.path; return true; });
Used together with window.onbeforeunload , you can very well leave the user to leave the page without confirmation.
Grinn source share