用户不友好的 onbeforeunload / onunload
我正在努力处理 onbeforeunload、onunload 并在正确的时间生成正确的消息。欢迎任何想法。情况是这样的:
- 当我使用 onbeforeunload 时,用户会收到一条消息来选择是否导航离开,但我无法将其关闭 - 该消息是标准消息,并且无论事件处理程序中的内容如何都会出现。
- 当我使用 onunload 时,我可以控制出现的“再见”消息,但我无法为用户提供改变主意的机会。
例如,如果有数据要保存。使用 onbeforeunload 时,无论用户数据的状态如何,都会弹出一条消息,询问他们是否要离开。使用onunload,我可以在询问用户之前选择消息并检查是否有未保存的数据,如果他们已保存,就省去了他们的麻烦,但我的用户无法选择留在页面上。
我在那里错过了什么吗?看来我无法获得我想要的简单行为 - 检查应用程序状态,发出消息,并让用户在离开之前采取行动。或者是否使用其中一个或两个事件来获得此行为?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我为你做了一个简单的例子,我认为它可以满足你的要求。不幸的是,无法更改
onbeforeunload
中的消息,但您可以在用户保存数据时删除该消息。 http://jsfiddle.net/JL48j/8/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/