为什么onbeforeunload事件没有触发 [英] Why onbeforeunload event is not firing

查看:747
本文介绍了为什么onbeforeunload事件没有触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在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&amp;Ms left, so eat more!<br>";
        } else {
            output += "Getting low on M&amp;Ms, take it easy!<br>";
        }
      }
      output += "All out of M&amp;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 事件的操作:


  1. 直接在浏览器中或通过链接导航到另一页。

  2. 关闭当前浏览器窗口或标签页。

  3. 重新加载当前页面。

  4. 通过JavaScript中的位置对象操作当前加载页面的URL。

  5. 调用 window.navigate 方法。

  6. 调用 window.open document.open 方法在同一窗口中打开文档。

  1. Navigating to another page directly in the browser or via a link.
  2. Closing the current browser window or tab page.
  3. Reloading the current page.
  4. Manipulating the URL of the currently loaded page through the location object from JavaScript.
  5. Invoking the window.navigate method.
  6. Invoking the window.open or the document.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屋!

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