Javascript window.onunload 在 Chrome 浏览器中无法正常工作。谁能帮我?

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

window.onunload is not working properly in Chrome browser. Can any one help me?

javascriptbrowseronunloadwindow.onunload

提问by Imran

I have written this code

我已经写了这段代码

function winUnload() {
    alert("Unload Window");
    MyMethod();
}

window.onunload = function() { winUnload(); }

This code is working fine in IE and Firefox. But this code is not working in Chrome. Both the statements alert("Unload Window");and MyMethod();are not working.

此代码在 IE 和 Firefox 中运行良好。但是这段代码在 Chrome 中不起作用。双方的陈述alert("Unload Window");MyMethod();不工作。

回答by Armin

There are some actions which are not working in chrome, inside of the unload event. Alert or confirm boxes are such things.

在卸载事件内部,有些操作在 chrome 中不起作用。警报或确认框就是这样的东西。

But what is possible (AFAIK):

但是有什么可能(AFAIK):

  1. Open popups (with window.open) - but this will just work, if the popup blocker is disabled for your site
  2. Return a simple string (in beforeunload event), which triggers a confirm box, which asks the user if s/he want to leave the page.
  1. 打开弹出窗口(使用 window.open) - 但如果您的站点禁用了弹出窗口阻止程序,这将起作用
  2. 返回一个简单的字符串(在 beforeunload 事件中),它会触发一个确认框,询问用户是否要离开页面。

Example for #2:

#2 的示例:

$(window).on('beforeunload', function() {
    return 'Your own message goes here...';
});

Demo: http://jsfiddle.net/PQz5k/

演示:http: //jsfiddle.net/PQz5k/

回答by Carlos

I know this is old but I found the way to make unload work using Chrome

我知道这很旧,但我找到了使用 Chrome 进行卸载工作的方法

window.onbeforeunload = function () {
  myFunction();
};

回答by Tracy

Armin's answer is so useful, thank you. #2 is what's most important to know when trying to set up unload events that work in most browsers: you cannot alert() or confirm(), but returning a string will generate a confirm modal.

阿明的回答很有用,谢谢。#2 是在尝试设置适用于大多数浏览器的卸载事件时最重要的知识:您不能使用 alert() 或 confirm(),但返回一个字符串将生成一个确认模式。

But I found that even with just returning a string, I had some cross-browser issues specific to Mootools (using version 1.4.5 in this instance). This Mootools-specific implementation worked great in Firefox, but did not result in a confirm popup in Chrome or Safari:

但是我发现即使只返回一个字符串,我也有一些特定于 Mootools 的跨浏览器问题(在本例中使用版本 1.4.5)。这个 Mootools 特定的实现在 Firefox 中运行良好,但在 Chrome 或 Safari 中没有导致确认弹出:

window.addEvent("beforeunload", function() {
    return "Are you sure you want to leave this page?";
});

So in order to get my onbeforeonload event to work across browsers, I had to use the JavaScript native call:

所以为了让我的 onbeforeonload 事件跨浏览器工作,我不得不使用 JavaScript 本机调用:

window.onbeforeunload = function() {
    return "Are you sure you want to leave this page?";
}

Not sure why this is the case, or if it's been fixed in later versions of Mootools.

不知道为什么会这样,或者它是否已在 Mootools 的更高版本中修复。

回答by Nivedittan

Please try window.onbeforeunloadinstead for window.onunloadfor chrome. You can also try calling onbeforeunloadfrom the body>tag which might work in chrome.

请尝试window.onbeforeunload代替window.onunloadfor chrome。您还可以尝试从body>标记调用onbeforeunload,这可能适用于 chrome。

However, we do have a problem with unload function in chrome browser. please check

但是,我们在 chrome 浏览器中的卸载功能确实存在问题。请检查

location.href does not work in chrome when called through the body/window unload event

通过正文/窗口卸载事件调用时,location.href 在 chrome 中不起作用

Thanks, Nived

谢谢,尼维德

回答by Leonardo Calzavara

回答by Amulya Kashyap

This works :

这有效:

var unloadEvent = function (e) {
    var confirmationMessage = "Warning: Leaving this page will result in any unsaved data being lost. Are you sure you wish to continue?";
    (e || window.event).returnValue = confirmationMessage; //Gecko + IE
    return confirmationMessage; //Webkit, Safari, Chrome etc.
};
window.addEventListener("beforeunload", unloadEvent);

回答by Shelby Moore III

The onunload event won't fire if the onload event did not fire. Unfortunately the onload event waits for all binary content(e.g. images) to load, and inline scripts run beforethe onload event fires. DOMContentLoadedfires when the page is visible, before onload does. And it is now standard in HTML 5, and you can test for browser supportbut note this requires the <!DOCTYPE html>(at least in Chrome). However, I can not find a corresponding event for unloading the DOM. And such a hypothetical event might not work because some browsers may keep the DOM around to perform the "restore tab" feature.

如果 onload 事件未触发,则 onunload 事件不会触发。不幸的是onload 事件等待所有二进制内容(例如图像)加载,并且在 onload 事件触发之前运行内联脚本DOMContentLoaded在页面可见时触发,在 onload 之前。它现在是 HTML 5 的标准,您可以测试浏览器支持,但请注意这需要<!DOCTYPE html>(至少在 Chrome 中)。但是,我找不到卸载 DOM 的相应事件。并且这种假设事件可能不起作用,因为某些浏览器可能会保留 DOM 以执行“恢复选项卡”功能。

The only potential solution I found so far is the Page Visibility API, which appears to require the <!DOCTYPE html>.

到目前为止,我找到的唯一可能的解决方案是Page Visibility API,它似乎需要<!DOCTYPE html>.