问题
I'm struggling with onbeforeunload, onunload and generating the right messages at the right time. Any idea welcome. This is the situation:
- when I use onbeforeunload, the user gets a message to choose whether to navigate away, but I can't turn it off - the message is standard and appears regardless what's in the event handler.
- when I use onunload, I can control the "bye" messages that occur, but I cant offer the user the opportunity to change their mind.
So for example, if there is data to save. With onbeforeunload, a message pops up, -regardless of the state of the user's data- asking if they want to navigate away or not. With onunload, I can choose the message and check whether there is unsaved data before asking the user, saving them the trouble if they have saved, but my user can't choose to stay on the page.
Am I missing something there? It seems I can't get the simple behaviour I want - check application status, give a message, and offer the user to take action before they leave. Or is there a use of either or both events to get this behaviour?
回答1:
I made a simple example for you, that I think might do what you want. Unfortunately there is no way to change the message in onbeforeunload
, but you can remove the message when the user saves data. http://jsfiddle.net/JL48j/8/
来源:https://stackoverflow.com/questions/8627090/user-unfriendly-onbeforeunload-onunload