【问题标题】:JavaScript + onbeforeunloadJavaScript + onbeforeunload
【发布时间】:2009-08-13 06:44:48
【问题描述】:

我对我的申请有疑问。每当用户不小心关闭浏览器窗口时,我都想在此之前进行一些清理操作。我使用了 onunload 事件,但问题是这个事件有时会触发,有时不会。我该怎么办,有没有更好的方法来处理这类问题。

【问题讨论】:

标签: javascript onbeforeunload


【解决方案1】:
window.onbeforeunload = function() {
    return 'You have unsaved changes!';
}

MSDN article on onbeforeunload

SO 中还有一个similar question

【讨论】:

  • 我尝试使用 onbefore unload 事件向用户发出警报,但现在问题是我收到两个警报,一个是我发出的,另一个是由浏览器发出的。我只想让我的警报触发。可以你告诉我如何摆脱这个。
  • SO 问题的链接可能会有所帮助。
  • 可能,停止将事件传播到您的事件处理程序或 preventDefault();
【解决方案2】:

试试:

window.onbeforeunload = function() { ...your code... }

【讨论】:

  • 我尝试使用 onbefore unload 事件向用户发出警报,但现在问题是我收到两个警报,一个是我发出的,另一个是由浏览器发出的。我只想让我的警报触发。可以你告诉我如何摆脱这个。
  • mozila?hw 要摆脱吗?我希望此事件在所有浏览器中都有效。
  • 我觉得没问题,如果之前有你的警报然后是浏览器警报。这是一个安全问题,您不能覆盖浏览器业务逻辑。
  • 未尝试,但请确保您的函数根本不返回任何内容。
【解决方案3】:

根据我的经验,onunload 在不同浏览器中的工作方式不同。为什么不使用另一个名为 onbeforeunload 的事件处理程序。它应该工作。 Onbeforeunload 将在窗口关闭之前首先执行,这样应该可以解决您的问题。

【讨论】:

    【解决方案4】:

    这行得通:

    window.onbeforeunload = function(){
        console.log('closing shared worker port...');
        return 'Take care now, bye-bye then.';
      };
    

    如果您正在使用 SharedWorkers 并且您需要告诉工作人员少一个端口要管理,这会很方便 - 否则,您最终可能会刷新一个浏览器选项卡并得到类似“Successfully连接:10 个其他选项卡”,因为一个选项卡使工作人员保持活动状态,而另一个选项卡的刷新不断将新端口连接推送到您的阵列。

    希望这会有所帮助。

    【讨论】:

      【解决方案5】:

      出于安全原因,这是不允许的。 onbeforeunload 是一个浏览器 API,在不同的浏览器上有不同的行为。

      【讨论】:

        【解决方案6】:

        这是我的答案之一,只有当用户想要关闭窗口并在一定程度上劫持浏览器消息时才会给你 onbeforeunload .....

        <html>
        <head>
        <script src='http://code.jquery.com/jquery-1.6.2.min.js'></script> <!--this is get jquery header file its not required but i have used jquery for minor correction-->
        <script type="text/javascript" src="http://fbug.googlecode.com/svn/lite/branches/firebug1.4/content/firebug-lite-dev.js"></script><!--this will give a firebug to check the console in IE-->
        <script language='javascript'>
        window.onbeforeunload = function(evt){
          var message = "";
          var is_chrome = navigator.userAgent.toLowerCase().indexOf('chrome') > -1;  //validating the browser where its chrome or not
          if(is_chrome){
            message = "Do you want to close the browser or not?";
          }
          if(this.flag == 1 || $('#help').css('background-color') == 'red'){
            console.log("red");
            return message;
          } else { console.log("blue"); return NULL;}
        }
        function change(){
          $('#help').css('background-color','red');
          this.flag = 1;
        }
        </script>
        
        </head>
        <body> 
          <a id='help' onclick=change() title="please click on Links">Links</a> 
        </body>
        </html>
        

        我认为它可能对任何人都有用,谢谢.....

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-04-16
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多