Cancel a backward or forward event that causes a hash change or HTML 5 history change

Is it possible to cancel a repeated or reverse browser event that results in a hash change or a change in the history of HTML 5, in the same way that you can cancel a page load or forward an event through window.onbeforeunload?

+3
source share
1 answer

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.

0
source

Source: https://habr.com/ru/post/904730/


All Articles