OnUnload警告错误“NS_ERROR_NOT_AVAILABLE” [英] OnUnload Alert Error "NS_ERROR_NOT_AVAILABLE"

查看:188
本文介绍了OnUnload警告错误“NS_ERROR_NOT_AVAILABLE”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<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>

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

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.

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

alert("test");

我没有尝试提醒测试变量,只是在任何人试图指向之前的文本test那个。

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

推荐答案

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

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.


  • 这个的好处是弹出消息有2个按钮:确定和取消。

  • 如果用户点击确定,浏览器将继续离开页面

  • 如果用户点击取消,浏览器将取消卸载并将保留在当前页面上

  • onbeforeunload事件是唯一可以取消onunload事件的弹出窗口

  • 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

以下是一个例子:

<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>

您似乎正在尝试在onunload事件中发出警报。这里的问题是,为时已晚。页面已经卸载,没有停止。您可能会收到要显示的提醒消息,但用户点击的内容并不重要,因为该页面已经卸载。

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.

您最好的选择是去使用onbeforeunload事件。

Your best bet is to go with the onbeforeunload event.

这篇关于OnUnload警告错误“NS_ERROR_NOT_AVAILABLE”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆