Javascript onunload 在 Chrome 和 safari 中不起作用

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

onunload not working in Chrome and safari

javascripthtmlwebkit

提问by prasanth

I have parent page and child page. In child page body tag i have below code.

我有父页面和子页面。在子页面正文标签中,我有以下代码。

<body onunload="window.g_autoclosed=true;if(window.opener&&window.opener.NavModelessClose){window.opener.NavModelessClose(window,false);}"> 

when i click on button in parent page, child page should close and call the onunload event.

当我点击父页面中的按钮时,子页面应该关闭并调用 onunload 事件。

The unload event is raising correctly in IE and FF. But it is not raising in chrome and safari.

卸载事件在 IE 和 FF 中正确引发。但它并没有在 chrome 和 safari 中提高。

Please help me.

请帮我。

回答by Joachim Sauer

Modern WebKit browsers don't necessarily fire the unloadevent at the moment where the page is hidden. This is done in order to allow improved caching.

现代 WebKit 浏览器不一定会unload在页面隐藏时触发事件。这样做是为了允许改进缓存。

You might consider replacing the use of unloadwith the pagehideevent.

您可能会考虑将使用替换unloadpagehide事件。

See this blog post for an in-depth discussion.

有关深入讨论,请参阅此博客文章

回答by user1769609

Unload works in modern browsers, but for example you cant launch an alert. See: http://bugs.jquery.com/ticket/10509

卸载适用于现代浏览器,但例如您无法启动警报。请参阅:http: //bugs.jquery.com/ticket/10509

Also if you want to use $.get or $.post in unload, is better that you use $.ajax with async=false.

此外,如果您想在卸载时使用 $.get 或 $.post,最好使用带有 async=false 的 $.ajax。

Ex:

前任:

$(window).unload(function() {
            $.ajax({
              url: '/some_url.htm',
              async: false
            });
    });

回答by zatatatata

Maybe try onbeforeunloadevent?

也许尝试onbeforeunload事件?

回答by Assaf Moldavsky

yea onbeforeunload attached to window (or inner window of the child page) should do it

是的 onbeforeunload 附加到窗口(或子页面的内部窗口)应该这样做