Javascript addEventListener 不适用于 onbeforeunload

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10919790/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 03:34:22  来源:igfitidea点击:

addEventListener not working with onbeforeunload

javascripteventsgoogle-chromeaddeventlisteneronbeforeunload

提问by zconnelly13

window.addEventListener("onbeforeunload",function() {return "are you sure?"});

^ This does not seem to work... at all... the page will simply close without displaying the confirmation box...

^ 这似乎不起作用......根本......页面将简单地关闭而不显示确认框......

I realize that...

我意识到...

window.onbeforeunload = function() {return "are you sure?"}

Will work, but I want to add to the functionality (e.g. add many event listeners to the "onbeforeunload" function) not rewrite the function completely!

将工作,但我想添加到功能(例如,将许多事件侦听器添加到“onbeforeunload”函数)而不是完全重写该函数!

回答by meder omuraliev

Remove the onfrom onbeforeunload.

取出ononbeforeunload

Also, be aware that addEventListenerwill not work in the older IE's and possibly other browsers. If you want consistent event binding use a library.

另外,请注意,这addEventListener在较旧的 IE 和可能的其他浏览器中不起作用。如果您想要一致的事件绑定,请使用库。

回答by user7610

There is an "almost cross-browser working example" at Mozila Developer Network API reference for beforeunload event. Use their code.

在 Mozila Developer Network API 参考中有一个“几乎跨浏览器的工作示例”,用于 beforeunload 事件。使用他们的代码。

in 2014, that was

在 2014 年,那是

window.addEventListener("beforeunload", function (e) {
  var confirmationMessage = "\o/";

  (e || window.event).returnValue = confirmationMessage;     //Gecko + IE
  return confirmationMessage;                                //Webkit, Safari, Chrome etc.
});

in 2020, it now is

在 2020 年,现在是

window.addEventListener('beforeunload', (event) => {
  // Cancel the event as stated by the standard.
  event.preventDefault();
  // Chrome requires returnValue to be set.
  event.returnValue = '';
});

all of the above?

上述所有的?

If I ever needed this, I'd want to entrust the job to a library. If I had to do this myself, I imagine one can do all of the above, just to be extra sure

如果我需要这个,我想把这项工作委托给图书馆。如果我必须自己做这件事,我想我可以做到以上所有,只是为了更加确定

  • do not try to set meaningful message text, it will only give inconsistent UX
  • event = event || window.event
  • event.preventDefault(), maybe after check that preventDefault is defined?
  • event.returnValue = ''
  • return ''
  • 不要尝试设置有意义的消息文本,它只会给出不一致的用户体验
  • event = event || window.event
  • event.preventDefault(),也许在检查了 preventDefault 的定义之后?
  • event.returnValue = ''
  • return ''

回答by Tayeaba Mila

There's no prefix onfor the EventListenersbut it's applicable or may I say necessary for EventHandlers

EventListeners没有前缀on,但它是适用的,或者我可以说EventHandlers 是必要的

So, just keep in mind that

所以,请记住

EventHandlers = prefix on

EventListeners = prefix off

EventHandlers =前缀

EventListeners =前缀关闭