JavaScript 问题:Onbeforeunload 还是 Onunload?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3775566/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JavaScript question: Onbeforeunload or Onunload?
提问by pratyk
So I want to store some information in localstorage of a browser when the page is refreshed or the user exits the page for future use. I figured that I'd use some Javascript to detect the event and store the state of the page visit.
所以我想在页面刷新或用户退出页面时将一些信息存储在浏览器的localstorage中以备将来使用。我想我会使用一些 Javascript 来检测事件并存储页面访问的状态。
Now my predicament is: shall i use Onbeforeunload or Onunload method to accomplish this objective?
现在我的困境是:我应该使用 Onbeforeunload 还是 Onunload 方法来实现这个目标?
Thanks
谢谢
采纳答案by Dagg Nabbit
Why not register it with both just to be on the safe side? Just have the listener do a check to see if the data's stored yet and exit early.
为什么不向两者注册它只是为了安全起见?只需让侦听器检查数据是否已存储并提早退出。
Here's a quick example using event properties:
这是一个使用事件属性的快速示例:
window.onunload = window.onbeforeunload = (function(){
var didMyThingYet=false;
return function(){
if (didMyThingYet) return;
didMyThingYet=true;
// do your thing here...
}
}());
Or you could use attachEvent:
或者你可以使用 attachEvent:
(function(){
var didMyThingYet=false;
function listener (){
if (didMyThingYet) return;
didMyThingYet=true;
// do your thing here...
}
window.attachEvent("onbeforeunload", listener);
window.attachEvent("onunload", listener);
}());
回答by Juan Mendes
The proper place to do it is onunload. onbeforenload is meant to be used when you may need to stop the user from leaving the page, like when there is unsaved data.
执行此操作的正确位置是 onunload。onbeforenload 用于在您可能需要阻止用户离开页面时使用,例如有未保存的数据时。
I would be weary of writing code to cover all cases because you're not willing to test all cases. If you do your research and find that's it's too complicated across browsers, yeah, go ahead and do as much as you can. However, I believe there is no problem with doing local storage onunload. I actually did run into a problem, trying to send information to the server onunload. That did not happen reliably across browsers so I did end up with an approach that used both unload and beforeunload, but only after due diligence.
我会厌倦编写涵盖所有情况的代码,因为您不愿意测试所有情况。如果您进行研究并发现跨浏览器太复杂了,是的,请继续并尽可能多地做。但是,我相信在卸载时进行本地存储没有问题。我确实遇到了一个问题,试图在卸载时将信息发送到服务器。这在浏览器中并没有可靠地发生,所以我最终采用了一种同时使用卸载和卸载前的方法,但只是在尽职调查之后。
So my suggestion is do it onunload, check all your browsers.
所以我的建议是在卸载时执行,检查所有浏览器。
回答by Milan Aleksi?
From my experience: use onunloadsince Opera does not support the other event. Yes, I know... Opera... but still :)
根据我的经验:使用,onunload因为 Opera 不支持其他事件。是的,我知道......歌剧......但仍然:)
回答by VinayC
I believe that Onbeforeunload is a safe bet. It is possible for browser to not execute onunload script completely - for example, if you have alert in onunload, chrome would not show if you close the window.
我相信 Onbeforeunload 是一个安全的赌注。浏览器可能无法完全执行 onunload 脚本 - 例如,如果您在 onunload 中有警报,则关闭窗口时 chrome 不会显示。

