Alert user when they hit the browser back button - with good reason

帅比萌擦擦* 提交于 2020-01-01 08:45:46

问题


I know this borders on the taboo here, and please don't reply with "you should never do this", etc.

I have a very long form in a wizard, and some users are too used to using the browser's back and forward buttons that they use those instead of the "Back" and "Next" buttons on the form wizard. If they hit the browser's back button, they will lose all of their form data (which is a pain in the ass, since the form is so long).

Is it possible to display an alert that when will have a "take me out of here" button and a "cancel" button, so if they hit cancel it will cancel the function of the back button?


回答1:


You should return a message from the onbeforeunload event, like this:

window.onbeforeunload = function() {
    return "Leaving this page will reset the wizard";
};

Note that this event will fire when the user leaves the page for any reason, even after your wizard finishes.
You should set a flag when the wizard finishes and not return a message.




回答2:


You can use the jQuery BBQ plugin to rewrite the functionality of the next/previous buttons to make it more seemless transitioning throughout this "wizard"

http://benalman.com/projects/jquery-bbq-plugin/

Though it is a bit complex to setup, take a look at the examples provided if you're looking for the same functionality.

Chances are you'll probably have to refactor your code to accommodate, and it will take hours.



来源:https://stackoverflow.com/questions/3033874/alert-user-when-they-hit-the-browser-back-button-with-good-reason

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!