javascript OnUnload 警报错误“NS_ERROR_NOT_AVAILABLE”

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

OnUnload Alert Error "NS_ERROR_NOT_AVAILABLE"

javascriptalertonunloadwindow.onunload

提问by Cains

<html>
<body>

<button type="button" onclick="clickme()">Click Me</button>

<script>
var test = 0;

function clickme() {
  test = 1;
  console.log(test);
}

window.onunload = function() {
  alert("test");
}
</script>

</body>
</html>

I'm using this simple code to test some things with onunload and onbeforeunload. For some reason whenever I refresh/leave the page and cause the onunload event I get no alert and an error in the Firebug console. If I use onbeforeunload this works and I get no error, but I hear onbeforeunload isn't very good cross-browser.

我正在使用这个简单的代码来测试 onunload 和 onbeforeunload 的一些东西。出于某种原因,每当我刷新/离开页面并导致 onunload 事件时,我都不会在 Firebug 控制台中收到警报和错误。如果我使用 onbeforeunload 这可以工作并且没有错误,但是我听说 onbeforeunload 不是很好的跨浏览器。

NS_ERROR_NOT_AVAILABLE: Component returned failure code: 0x80040111     
(NS_ERROR_NOT_AVAILABLE) [nsIDOMWindow.alert]

alert("test");

I am not trying to alert the test variable, just the text "test" before anyone tries to point that out.

我不是想提醒测试变量,只是在任何人试图指出之前的文本“测试”。

回答by Brian

If you want that to work, it will have to be in the onbeforeunload event, but instead of creating an alert/confirm pop-up, the onbeforeunload event has a built in pop-up. All you have to do is return a string and the pop-up will appear when the user tries to navigate away from the page. If there is no return variable, there will be no pop-up.

如果你想让它工作,它必须在 onbeforeunload 事件中,但不是创建警报/确认弹出窗口,onbeforeunload 事件有一个内置的弹出窗口。你所要做的就是返回一个字符串,当用户试图离开页面时,弹出窗口就会出现。如果没有返回变量,就不会弹出。

  • The great thing with this is that the pop-up message has 2 buttons: OK and Cancel.
  • If the user hits OK, the browser will continue to navigate away from the page
  • If the user hits Cancel, the browser will cancel the unload and will stay on the current page
  • The onbeforeunload event is the only pop-up that can cancel the onunload event
  • 这样做的好处是弹出消息有 2 个按钮:确定和取消。
  • 如果用户点击 OK,浏览器将继续离开页面
  • 如果用户点击取消,浏览器将取消卸载并停留在当前页面
  • onbeforeunload 事件是唯一可以取消 onunload 事件的弹窗

An example is below:

一个例子如下:

<script type="text/javascript">

window.onbeforeunload=before;
window.onunload=after;

function before(evt)
{
   return "This will appear in the dialog box along with some other default text";
   //If the return statement was not here, other code could be executed silently (with no pop-up)
}

function after(evt)
{
   //This event fires too fast for the application to execute before the browser unloads
}

</script>

It seems like you are trying to do an alert in the onunload event. The issue here is that it's too late. The page is already unloading and there is no stopping it. You may be able to get an alert message to show, but it doesn't matter what the user clicks because the page is already unloading.

您似乎正在尝试在 onunload 事件中发出警报。这里的问题是为时已晚。该页面已经在卸载并且无法阻止它。您可能会收到要显示的警报消息,但用户单击什么并不重要,因为页面已经在卸载。

Your best bet is to go with the onbeforeunload event.

最好的办法是使用 onbeforeunload 事件。