多个onbeforeunload()和onunload()事件 [英] Multiple onbeforeunload() and onunload() events

查看:85
本文介绍了多个onbeforeunload()和onunload()事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从客户端遇到一个奇怪的问题,我们的代码,包括使用onbeforeunload()来触发对话框,但它们还包括另一个公司代码,它们也绑定了这个事件处理程序。

I have a strange issue from a client in that our code, which they include uses onbeforeunload() to trigger a dialog, but they are also including another companies code which also binds this event handler.

两者可以同时执行吗?

Is it possible for both to execute at the same time?

我一直在读这个, http:// oreilly .com / catalog / 9780596101992 JavaScript:The Definitive Guide,Fifth Edition试图帮助更好地理解浏览器内部和javascript堆栈中发生的事情,但它确实令人费心。

I've been reading this, http://oreilly.com/catalog/9780596101992 "JavaScript: The Definitive Guide, Fifth Edition" to try and help better understand what is happening in the browsers internals and the javascript stack, but it's proving quite mind bending.

我从阅读本书中了解到,如果使用Level 2 API addEventListener()附加一些事件可以同时执行,但订单将由浏览器完成。但是没有提到onbeforeunload()事件。只是onunload()。

I understand from reading the book that some events can be executed all at the same time if they are attached using the Level 2 API addEventListener() but the order will be up to the browser. However there is no mention of the onbeforeunload() event. Just the onunload().

这引出了我的第二部分问题。如果在onbeforeunload()中触发了一个事件,我认为除非它返回true,否则onunload()永远不会被调用?

Which leads me to the second part of the question. If an event is triggered in onbeforeunload() am I right in thinking that unless it returns true, the onunload() will never be called?

如果有人可以放弃一些亮点在它上面,或者给我一个很好的教程/指南,让你有多个事件处理程序分配给同一个事件,或者特别是这两个事件就是ace。

If anyone can shed some light on it, or hook me up with a nice tutorial/guide on either having multiple event handlers assigned to the same event, or specifically on these two events that would be ace.

谢谢

推荐答案


两者是否可以同时执行?

Is it possible for both to execute at the same time?

同时不字面,没有 —浏览器中的Javascript(目前)是单线程的。因此, onbeforeunload 事件可以有多个处理程序,但它们将被串行调用,而不是同时调用。至少在理论上;在实践中,看起来只有其中一个被调用(见下文)。

Not literally at the same time, no — Javascript in browsers is (currently) single-threaded. So there can be multiple handlers for the onbeforeunload event, but they'll be called serially, not simultaneously. At least in theory; in practice, it looks like only one of them is called (see below).


如果在onbeforeunload()中触发了一个事件我我认为除非它返回true,否则永远不会调用onunload()?

If an event is triggered in onbeforeunload() am I right in thinking that unless it returns true, the onunload() will never be called?

如果有任何 onbeforeunload 处理程序取消卸载,不会调用 onunload 处理程序。您通过执行两项操作取消卸载(因为浏览器在这里有所不同):首先,您将字符串分配给事件的 returnValue 属性 object,然后将该字符串从函数中返回。详细信息此处这里。 (该字符串用作提示,允许用户决定是否取消卸载。)

If any onbeforeunload handler cancels the unload, no onunload handler will be called. You cancel the unload by doing two things (because browsers differ here): First, you assign a string to the returnValue property of the event object, and then you return that string out of the function. Details here and here. (The string is used as a prompt, allowing the user to decide whether to cancel the unload.)

理论上,让我们看一下实际发生的事情:

So much for theory, let's look at what actually happens:

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-type" content="text/html;charset=UTF-8">
<title>Test Page</title>
<style type='text/css'>
body {
    font-family: sans-serif;
}
</style>
<script type='text/javascript'>
window.onload = pageInit;
function pageInit() {
    hook(window, 'beforeunload', beforeUnload1);
    hook(window, 'beforeunload', beforeUnload2);
    hook(window, 'unload', unload1);
    hook(window, 'unload', unload2);
}

function beforeUnload1(event) {
    var s;

    event = event || window.event;
    s = "Message from beforeUnload1";
    event.returnValue = s
    return s;
}

function beforeUnload2(event) {
    var s;

    event = event || window.event;
    s = "Message from beforeUnload2";
    event.returnValue = s
    return s;
}

function unload1(event) {
    alert("Message from unload1");
}

function unload2(event) {
    alert("Message from unload2");
}

var hook = (function() {
    var d;

    function hookViaAttachEvent(obj, eventName, handler) {
        obj.attachEvent('on' + eventName, handler);
    }
    function hookViaAddEventListener(obj, eventName, handler) {
        obj.addEventListener(eventName, handler, false);
    }

    d = document.createElement('span');
    if (d.addEventListener) {
        return hookViaAddEventListener;
    }
    else if (d.attachEvent) {
        return hookViaAttachEvent;
    }
    throw "Neither attachEvent nor addEventListener found.";
})();
function hook(eventName, handler) {

}
</script>
</head>
<body></body>
</html>

在Chrome,IE和Firefox上,我只看到来自其中一个<$ c的通知$ c> onbeforeunload 处理程序,即使我说它可以继续并离开。我希望这可能是因为否则,一个充满刺激性的页面可能只是注册了一堆处理程序并且一直唠叨用户留在页面上。

On Chrome, IE, and Firefox, I only ever see a notification from one of the onbeforeunload handlers, even when I say it's okay to go ahead and leave. I expect this is probably because otherwise, a sufficiently-irritating page could just register a bunch of handlers and keep nagging the user to stay on the page.

之后(一个)问题,如果我允许导航继续,我会收到两个卸载消息。

After the (one) question, if I allow navigation to continue, I get both unload messages.

这篇关于多个onbeforeunload()和onunload()事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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