javascript - 在离开网页之前提示用户填写表格 - 通过 Javascript

标签 javascript onbeforeunload

我的网页上有一个网络表单。在用户离开页面之前,我想要一条 javascript 弹出消息,询问用户是否已完成带有"is"或“否”按钮的表单。如果用户点击"is",那么他们将被带到他们打算去的页面。但是,如果用户单击“否”,则用户会留在该页面上。

我对 Javascript 不是很熟悉,因此非常感谢任何指导。我怀疑我会使用类似下面的东西:

<ELEMENT onbeforeunload = "handler" ... >

提前致谢。

拆分 P.

最佳答案

我不确定这是别人要求你做的事情,还是你认为是个好主意,但我要说的是,如果你有选择,请不要这样做。如果用户想离开页面,让他们离开页面,不要告诉他们他们已经知道的事情。此外,一些现代浏览器已经内置了此功能,因此它只是为用户添加了另一层烦人的东西。

话虽如此,看看this page ,它将告诉您您需要知道的一切。

What makes onbeforeunload quirky is that if you return ANYTHING at all in your event handler it will pop up a confirmation alert box asking the user if he or she is REALLY sure they want to leave the page

...

You can insert your own explanatory text BETWEEN those two lines by including a string on the return statement. For instance:

<script type="text/javascript">
  window.onbeforeunload = function () {
    return "The text you want the alert box to say.";
  }
</script>

关于javascript - 在离开网页之前提示用户填写表格 - 通过 Javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3451127/

相关文章:

javascript - 使用 onbeforeunload 关闭窗口之前的等待事件

javascript - 在卸载/离开页面之前检查给定表单中的更改

javascript - 对 IE 和 FF 使用 JQuery 或 onbeforeunload

javascript - ExtJS 当查询执行时间超过超时时捕获事件

javascript - 在 JavaScript 中编写嵌入式 Ruby

javascript - 如何在 HTML5 的 "video"标签中使用 youtube 视频作为源

javascript - 检查页面刷新是否触发了window.onbeforeunload?

javascript - 找不到管道 'safeResourceUrl'

javascript - 获取 JSON 文件中的前 10 个值

web-applications - 使用 ajax 调用下载文件时避免触发 window.onbeforeunload