【问题标题】:'beforeunload' event only fires in firefox when I click on the page'beforeunload' 事件仅在我单击页面时在 Firefox 中触发
【发布时间】:2016-08-07 18:32:13
【问题描述】:

我使用 window.open() 打开一个新窗口

然后我试图在用户关闭打开的窗口时运行一些脚本(一个 ajax 调用)。

打开工作正常,但是当我尝试关闭窗口时,代码在 FF 中没有按预期工作

这是我的代码

<?php

echo '<div id="iframecontainer"><iframe name="meeting-iframe" frameborder="0" style="width:80%;height:80%" src="' . $link . '"></iframe></div>';


?>
<script>
(function ($) {

  $(document).ready(function() {


    // Variables from php
    var event_id = '<?php echo $event_id; ?>';
    var user_id = '<?php echo $current_user->uid; ?>';
    //////////////////////////////////////////////////////////////////////
    // Run function before window is closed,doesn't work as expected in FF
    window.addEventListener("beforeunload", function (e) {

      var e = e || window.event;
      var message = 'Are you sure you want to leave the meeting?';


      if(e) {
        e.returnValue = message;
      }


     // Post to get meeting info
      $.post('/easymeet/getInfo/' + event_id).done(function(meetResponse) {
        //ajax callback ,I have hidden the code here as I don't think its relevent to the question

      });
    });

  });
}(jQuery))

我正在我的窗口中加载一个 iframe,而 iframe 正在获取的页面全是 flash(只是指出)。

我正在 IE、chrome 和 Firefox 中进行测试 这里发生的是 beforeunload 被触发并且在 IE 和 Chrome 中运行良好。

但在 Firefox 中,我手动必须单击 HTML 或进行一些交互。如果我尝试关闭窗口而不做任何事情,'beforeunload' 事件似乎根本不会触发。

【问题讨论】:

  • 有什么解决办法吗?
  • @IonicăBizău 你找到解决方案了吗?我抓挠我的头发
  • @slier 不,这只是 Firefox 的工作方式(除非您更改 about:config–– 如下所述––,您可能对此不感兴趣)。恐怕没有办法强制这样做。
  • @IonicăBizău 是的,我想是的 ..thx!

标签: javascript jquery html firefox iframe


【解决方案1】:

Firefox 有一些配置,您可以在导航工具栏中找到“about:config”。在那里你会发现一些标志,包括 dom.require_user_interaction_for_beforeunload,它只会在你点击页面时触发 'beforeunload' 事件。但是不能通过代码修改这个配置;您必须在 Firefox 中手动执行此操作。

PS:对不起我的英语。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-16
    • 2019-09-25
    • 2021-08-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多