onunload触发了多少次?

时间:2022-03-11 00:58:05

I have just been helped on a problem I have here.

我刚刚帮助解决了我遇到的问题。

var win = window.open(url, name);
win.onunload = StartLoad;
win.close();

To solve this problem completely, I wanted to know if onunload will be triggered once or every time a event occurs?

为了完全解决这个问题,我想知道是否会在事件发生一次或每次触发onunload?

In other words, will my function startLoad run every time the child window "win" gets redirected, closed etc? Or will it do this event once and that's it?

换句话说,每次子窗口“win”被重定向,关闭等时,我的函数startLoad会运行吗?或者它会做一次这个事件,就是这样吗?

Apologies, if this is a silly question.

道歉,如果这是一个愚蠢的问题。

Thanks all

4 个解决方案

#1


No - this method can fire multiple times as you navigate off a page in IE6 and IE7.

不 - 当您在IE6和IE7中导航页面时,此方法可以多次触发。

This code snippet illustrates this (save as OnUnloadTest.htm):

此代码段说明了这一点(另存为OnUnloadTest.htm):

<body>
    <form id="form" action="OnUnloadTest.htm" method="post">
        Click <a href="javascript:form.submit()">here</a>
    </form>
    <script type="text/javascript">
        window.onbeforeunload = beforeunload
        function beforeunload() {
            alert('OnUnload');
        }
    </script>
</body>

Basically, the event fires once for the actual anchor click, and once as the page actually posts back. I've only seen this issue when you have javascript in the href of the anchor, although if you use ASP.NET linkbuttons then be warned as this puts javascript in the href.

基本上,事件为实际锚定点击触发一次,并且在页面实际回发时触发一次。我只在锚点的href中有javascript时才看到这个问题,尽管如果你使用ASP.NET链接按钮然后会被警告,因为这会将javascript放入href中。

For most other sorts of navigation (e.g. user clicks a normal anchor, or closes the browser, or navigates away with a bookmark, etc) the event does only fire once.

对于大多数其他类型的导航(例如,用户点击普通锚点,或关闭浏览器,或使用书签导航等),事件仅触发一次。

#2


It should only fire once, the first time the window unloads. Anything else would be a security hole.

它应该只在窗口第一次卸载时触发一次。其他任何东西都是安全漏洞。

#3


If you want to make sure that your event handler only runs once you can have the handler unbind itself the first time it is invoked. This will guarantee that the callback does not run more than once:

如果你想确保你的事件处理程序只运行一次,你可以在第一次调用它时让处理程序解除绑定。这将保证回调不会运行多次:

var win = window.open(url, name);
win.onunload = function(event) {
    win.onunload = function() {}; // assign a noop
    return Startload.call(this, event);
};
win.close();

Some JavaScript libraries have a built-in helper for binding an event handler that you only want run once. For example, jQuery has a one() method for this purpose:

一些JavaScript库有一个内置的帮助器,用于绑定您只想运行一次的事件处理程序。例如,jQuery有一个one()方法用于此目的:

var win = window.open(url, name);
$(win).one('unload', Startload);
win.close();

#4


Read WebKit Page Cache II – The unload Event for interesting discussion on how unload event plays with page caching feature of modern browsers.

阅读WebKit页面缓存II - 卸载事件,用于讨论卸载事件如何与现代浏览器的页面缓存功能一起播放。

#1


No - this method can fire multiple times as you navigate off a page in IE6 and IE7.

不 - 当您在IE6和IE7中导航页面时,此方法可以多次触发。

This code snippet illustrates this (save as OnUnloadTest.htm):

此代码段说明了这一点(另存为OnUnloadTest.htm):

<body>
    <form id="form" action="OnUnloadTest.htm" method="post">
        Click <a href="javascript:form.submit()">here</a>
    </form>
    <script type="text/javascript">
        window.onbeforeunload = beforeunload
        function beforeunload() {
            alert('OnUnload');
        }
    </script>
</body>

Basically, the event fires once for the actual anchor click, and once as the page actually posts back. I've only seen this issue when you have javascript in the href of the anchor, although if you use ASP.NET linkbuttons then be warned as this puts javascript in the href.

基本上,事件为实际锚定点击触发一次,并且在页面实际回发时触发一次。我只在锚点的href中有javascript时才看到这个问题,尽管如果你使用ASP.NET链接按钮然后会被警告,因为这会将javascript放入href中。

For most other sorts of navigation (e.g. user clicks a normal anchor, or closes the browser, or navigates away with a bookmark, etc) the event does only fire once.

对于大多数其他类型的导航(例如,用户点击普通锚点,或关闭浏览器,或使用书签导航等),事件仅触发一次。

#2


It should only fire once, the first time the window unloads. Anything else would be a security hole.

它应该只在窗口第一次卸载时触发一次。其他任何东西都是安全漏洞。

#3


If you want to make sure that your event handler only runs once you can have the handler unbind itself the first time it is invoked. This will guarantee that the callback does not run more than once:

如果你想确保你的事件处理程序只运行一次,你可以在第一次调用它时让处理程序解除绑定。这将保证回调不会运行多次:

var win = window.open(url, name);
win.onunload = function(event) {
    win.onunload = function() {}; // assign a noop
    return Startload.call(this, event);
};
win.close();

Some JavaScript libraries have a built-in helper for binding an event handler that you only want run once. For example, jQuery has a one() method for this purpose:

一些JavaScript库有一个内置的帮助器,用于绑定您只想运行一次的事件处理程序。例如,jQuery有一个one()方法用于此目的:

var win = window.open(url, name);
$(win).one('unload', Startload);
win.close();

#4


Read WebKit Page Cache II – The unload Event for interesting discussion on how unload event plays with page caching feature of modern browsers.

阅读WebKit页面缓存II - 卸载事件,用于讨论卸载事件如何与现代浏览器的页面缓存功能一起播放。