问题描述
当我想要离开某些页面时,这是一个警告,Firefox会引发。根据我看到的页面,当我在填写表单后尝试关闭页面时出现此警告,我只能假设它正在动态页面上工作。使用哪种技术来实现此功能?我如何在一个简单的hello-world页面上自己实现它?
This is a warning that Firefox raises when I want to leave certain pages. Based on the pages I've seen this on and that this warning appears when I try to close the page after filling in the forms, I can only assume that it's working on a dynamic page. Which technology is used to implement this functionality? How can I implement it myself on a simple hello-world page?
推荐答案
你基本上为 beforeunload 事件。这允许您警告您的用户他们有未保存的数据。
You basically implement a handler for beforeunload
event. This allows you to warn your users that they have unsaved data.
伪代码:
window.onbeforeunload = function warnUsers()
{
if (needToConfirm)
{
// check to see if any changes to the data entry fields have been made
if(changesPresent)
return message to display
}
else {
// no changes - return nothing
}
}
}
这是一篇非常好的文章,深入讨论了这个问题:
Here's a very good article that discusses this in depth: http://www.4guysfromrolla.com/webtech/100604-1.shtml
注意:还有 onunload
事件但在页面之后触发卸载,因此采取任何可靠行动为时已晚。您永远不应该在 onunload
中放置任何关键代码,因为永远不会保证执行。
Note: There is onunload
event also but that fires after the page has unloaded, hence is too late to take any reliable action. You should never put any critical code in onunload
as that is never guranteed to execute.
这篇关于实施“此页面要求您确认是否要离开”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!