为什么onbeforeunload事件没有触发 [英] Why onbeforeunload event is not firing
问题描述
我在Chrome,FireFox和Safari上尝试了代码。仍然onbeforeunload不会发射。我也尝试了onunload,但它没有用。
I tried the code on Chrome, FireFox and Safari. Still the onbeforeunload does not fire. I also tried the onunload, but it did not work.
这是我正在试验的代码。
Here is the code I am experimenting with.
<!doctype html>
<html lang="en">
<head>
<title> Output to a Page </title>
<meta charset="utf-8">
<script>
window.onload = init;
window.onclick = clk;
window.onbeforeunload = closing;
function clk() {
window.alert("Ouch !!");
}
function closing() {
console.log("function alrt WORKS !!!!");
window.alert("closing now.....");
}
function init() {
var output = "";
for (var mms = 5; mms > 0; mms--) {
if (mms >= 3) {
output += "Still lots of M&Ms left, so eat more!<br>";
} else {
output += "Getting low on M&Ms, take it easy!<br>";
}
}
output += "All out of M&Ms";
var e = document.getElementById("output");
e.innerHTML = output;
}
</script>
</head>
<body>
<p id="output"></p>
</body>
</html>
推荐答案
onbeforeunload
由于安全原因,事件不可取消,但如果 onbeforeunload
事件的事件处理函数返回字符串值,则此文本将显示在确认对话框,用户可以确认是否要留下或离开当前页面。
The onbeforeunload
event is not cancel-able, because of security reasons, but if an event handler function for the onbeforeunload
event returns a string value, this text will be shown in a confirmation dialog box, where the user can confirm whether he wants to stay or leave the current page.
请注意,事件监听器无法为 onbeforeunload
事件注册 addEventListener
和 attachEvent
方法(只有Safari和谷歌Chrome支持)。对于跨浏览器解决方案,请在HTML中注册事件处理程序(使用 body
元素的 onbeforeunload
属性)或使用 onbeforeunload
窗口
对象的内联事件属性。有关详细信息,请参阅以下示例。
Note that event listeners cannot be registered for the onbeforeunload
event with the addEventListener
and attachEvent
methods (only Safari and Google Chrome support it). For a cross-browser solution, register the event handler in HTML (with the onbeforeunload
attribute of the body
element) or with the onbeforeunload
in-line event property of the window
object. See the examples below for details.
示例:
在HTML中:
< ELEMENT onbeforeunload =handler>
在JavaScript中:
In JavaScript:
object.onbeforeunload = handler;
object.addEventListener ("beforeunload", handler, useCapture);
调用 onbeforeunload
事件的操作:
- 直接在浏览器中或通过链接导航到另一页。
- 关闭当前浏览器窗口或标签页。
- 重新加载当前页面。
- 通过JavaScript中的位置对象操作当前加载页面的URL。
- 调用
window.navigate
方法。 - 调用
window.open
或document.open
方法在同一窗口中打开文档。
- Navigating to another page directly in the browser or via a link.
- Closing the current browser window or tab page.
- Reloading the current page.
- Manipulating the URL of the currently loaded page through the location object from JavaScript.
- Invoking the
window.navigate
method. - Invoking the
window.open
or thedocument.open
method to open a document in the same window.
尝试修改你的代码,如下所示:
Try to modify your code, like this:
window.onbeforeunload = closing;
/* other code here */
var closing = function () {
console.log("function alrt WORKS !!!!");
window.alert("closing now.....");
}
...或直接将代码直接放入 body
tag:
...or just put directly the code in your body
tag:
< body onbeforeunload =alert('function alrt WORKS !!!!') >
这篇关于为什么onbeforeunload事件没有触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!