gpt4 book ai didi

javascript - 实现 "this page is asking you to confirm that you want to leave"

转载 作者:可可西里 更新时间:2023-11-01 02:21:50 25 4
gpt4 key购买 nike

这是当我想离开某些页面时 Firefox 发出的警告。根据我在上面看到的页面以及当我在填写表格后尝试关闭页面时出现此警告,我只能假设它在动态页面上工作。哪种技术用于实现此功能?我如何在一个简单的 hello-world 页面上自己实现它?

最佳答案

您基本上为 beforeunload 事件实现了一个处理程序。这使您可以警告用户他们有未保存的数据。

伪代码:

  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
}
}
}

这里有一篇非常好的文章对此进行了深入讨论:http://www.4guysfromrolla.com/webtech/100604-1.shtml 注意:此链接没有存在的时间更长。这是来自 Wayback Machine 的副本: https://web.archive.org/web/20211020134123/http://www.4guysfromrolla.com/webtech/100604-1.shtml

注意:还有 onunload 事件,但它会在页面卸载后 触发,因此为时已晚,无法采取任何可靠的措施。永远不要在 onunload 中放置任何关键代码,因为它永远不会保证执行。

关于javascript - 实现 "this page is asking you to confirm that you want to leave",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6801494/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com