捕获 window.onbeforeunload
我有一个表单,其中输入字段保存在 onChange
中。在 Firefox (5) 中,即使窗口关闭,该功能也有效,但对于 Chrome 和 IE 则不然,即使他们在输入后尝试关闭窗口,我也需要确保保存这些数据一个字段,但 onBlur
事件尚未发生(即,他们已在文本框中输入了一些内容,但尚未从其中跳出)。
我已阅读以下有关使用 window.onbeforeunload
的文章: 第1条 文章 2
如果我使用以下内容:
window.onbeforeunload = function() {
return "onbeforeunload";
}
那么我会收到一个带有 onbeforeunload
中。
但是如果我尝试:
window.onbeforeunload = function() {
alert("onbeforeunload");
}
那么在任何浏览器中都不会发生任何事情,甚至是 Firefox。
我想要实现的是:
window.onbeforeunload = function() {
saveFormData();
}
如果有人能指出我可能出错的地方,我将不胜感激。
I have a form where the input fields are saved onChange
. In Firefox (5) this works even when the window is closed, but for Chrome and IE it doesn't and I need to be sure that I'm saving this data even if they try to close the window after they've typed in a field but an onBlur
event hasn't occurred (i.e. they've typed something into a textbox, but haven't tabbed out of it).
I have read the following SO articles on using window.onbeforeunload
:
article 1
article 2
if I use the following:
window.onbeforeunload = function() {
return "onbeforeunload";
}
then I get a popup with onbeforeunload
in.
but if I try:
window.onbeforeunload = function() {
alert("onbeforeunload");
}
then nothing happens in any browser, even Firefox.
what I want to achieve is:
window.onbeforeunload = function() {
saveFormData();
}
I'd be grateful if someone could point out where I might be going wrong.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
您必须从
onbeforeunload
中返回
:根据评论更新
,警报似乎不再适用于较新的版本,其他一切都可以: )
来自 MDN
还建议通过
addEventListener
接口使用它:更新后的代码现在如下所示:
You have to
return
from theonbeforeunload
:UPDATE
as per comments, alert does not seem to be working on newer versions anymore, anything else goes :)
FROM MDN
It is also suggested to use this through the
addEventListener
interface:The updated code will now look like this:
关于如何使用此事件似乎有很多错误信息(即使在本页上已投票的答案中也是如此)。
onbeforeunload
事件 API 是由浏览器出于特定目的而提供的:在此方法中唯一值得做的事情是返回一个字符串,然后浏览器将返回该字符串提示用户在离开该页面之前应采取行动。您无法阻止他们离开页面(想象一下这对最终用户来说将是一场噩梦)。由于浏览器使用确认提示向用户显示您从事件侦听器返回的字符串,因此您也无法在该方法中执行任何其他操作(例如执行 ajax 请求)。
在我编写的应用程序中,我想提示用户在离开页面之前让他们知道他们有未保存的更改。浏览器会向他们提示消息,之后,一切就不再由我掌控了,用户可以选择留下或离开,但此时您不再拥有对应用程序的控制权。
我如何使用它的一个示例(伪代码):
如果(且仅当)应用程序有未保存的更改,则浏览器会提示用户要么忽略我的消息(并无论如何离开页面),要么不离开页面。如果他们无论如何都选择离开该页面,那就太糟糕了,您对此无能为力(也不应该做)。
There seems to be a lot of misinformation about how to use this event going around (even in upvoted answers on this page).
The
onbeforeunload
event API is supplied by the browser for a specific purpose: The only thingyou can dothat's worth doing in this method is to return a string which the browser will then prompt to the user to indicate to them that action should be taken before they navigate away from the page. You CANNOT prevent them from navigating away from a page (imagine what a nightmare that would be for the end user).Because browsers use a confirm prompt to show the user the string you returned from your event listener, you can't do anything else in the method either (like perform an ajax request).
In an application I wrote, I want to prompt the user to let them know they have unsaved changes before they leave the page. The browser prompts them with the message and, after that, it's out of my hands, the user can choose to stay or leave, but you no longer have control of the application at that point.
An example of how I use it (pseudo code):
If (and only if) the application has unsaved changes, then the browser prompts the user to either ignore my message (and leave the page anyway) or to not leave the page. If they choose to leave the page anyway, too bad, there's nothing you can do (nor should be able to do) about it.
使用“alert()”时没有任何反应的原因可能是 MDN 所解释的:“HTML 规范规定对 window.alert()、window.confirm() 和 window.prompt() 方法的调用可能会被忽略在这次活动期间。”
但是,还有另一个原因可能导致您根本看不到警告,无论它是否调用alert(),也在同一站点上进行了解释:
“...浏览器可能不会显示在 beforeunload 事件处理程序中创建的提示,除非页面具有与之互动”
这就是我在当前版本的 Chrome 和 FireFox 中看到的情况。我打开我的页面,其中使用以下代码设置了 beforeunload 处理程序:
如果我不单击我的页面,换句话说“不与其交互”,然后单击关闭按钮,窗口将在没有警告的情况下关闭。
但是,如果我在尝试关闭窗口或选项卡之前单击页面,我确实会收到警告,并且可以取消窗口的关闭。
因此,这些浏览器是“智能的”(并且用户友好的),因为如果您没有对页面执行任何操作,则它不会有任何需要保存的用户输入,因此它们将关闭窗口而不会发出任何警告。
考虑一下,如果没有此功能,当您已经明确表示打算离开他们的网站时,任何网站都可能会自私地问您:“您真的想离开我们的网站吗?”。
看:
https://developer.mozilla.org/en-US/docs/Web /事件/卸载前
The reason why nothing happens when you use 'alert()' is probably as explained by MDN: "The HTML specification states that calls to window.alert(), window.confirm(), and window.prompt() methods may be ignored during this event."
But there is also another reason why you might not see the warning at all, whether it calls alert() or not, also explained on the same site:
"... browsers may not display prompts created in beforeunload event handlers unless the page has been interacted with"
That is what I see with current versions of Chrome and FireFox. I open my page which has beforeunload handler set up with this code:
If I do not click on my page, in other words "do not interact" with it, and click the close-button, the window closes without warning.
But if I click on the page before trying to close the window or tab, I DO get the warning, and can cancel the closing of the window.
So these browsers are "smart" (and user-friendly) in that if you have not done anything with the page, it can not have any user-input that would need saving, so they will close the window without any warnings.
Consider that without this feature any site might selfishly ask you: "Do you really want to leave our site?", when you have already clearly indicated your intention to leave their site.
SEE:
https://developer.mozilla.org/en-US/docs/Web/Events/beforeunload
我似乎来得有点晚了,而且更像是一个初学者,而不是任何专业人士;但这对我有用:
我将其作为内联脚本放置在我的 Head 和 Meta 元素之后,如下所示:
在我看来,该页面与发起者的要求高度相关(特别是标题为
window.html 的部分)。 onunload 和 window.onbeforeunload ):
https://javascript.info/onload-ondomcontentloaded
希望这会有所帮助。
I seem to be a bit late to the party and much more of a beginner than any expertise; BUT this worked for me:
I placed this as an inline script immediately after my Head and Meta elements, like this:
This page seems to me to be highly relevant to the originator's requirement (especially the sections headed
window.onunload
andwindow.onbeforeunload
):https://javascript.info/onload-ondomcontentloaded
Hoping this helps.
你只是不能在 onbeforeunload 中执行alert(),其他的都可以
you just cant do alert() in onbeforeunload, anything else works
要在用户离开页面时弹出消息以确认离开,您只需执行以下操作:
调用函数:
To pop a message when the user is leaving the page to confirm leaving, you just do:
To call a function:
是的,楼上大家都这么说。
对于您当前的情况,您可以使用 onInput(html5 中的新功能)来代替 onChange。输入事件是相同的,但它会在每次击键时触发,无论焦点如何。也适用于选择和所有其他操作,就像 onChange 一样。
Yes what everybody says above.
For your immediate situation, instead of onChange, you can use onInput, new in html5. The input event is the same, but it'll fire upon every keystroke, regardless of the focus. Also works on selects and all the rest just like onChange.