【问题标题】:Setting onbeforeunload on body element in Chrome and IE using jQuery使用 jQuery 在 Chrome 和 IE 中的 body 元素上设置 onbeforeunload
【发布时间】:2010-12-20 15:35:51
【问题描述】:

我有一个系统,我想与用户核实,一旦设置了脏标志,他们是否确定要离开页面。

我正在使用以下代码 - 在 FireFox 中,我可以通过 FireBug 查看页面源,并且标签中正确插入了 onbeforeunload 属性。

在 Chrome 和 FireFox 中,这不会发生,我可以在没有任何警告的情况下离开页面。更新 body 标签的 jQuery 行肯定正在执行,只是没有执行。

if ($("body").attr('onbeforeunload') == null) {
    if (window.event) {
        // IE and Chrome use this
        $("body").attr('onbeforeunload', 'CatchLeavePage(event)');
    }
    else {
        // Firefox uses this
        $("body").attr('onbeforeunload', 'return false;CatchLeavePage(event)');
    }
}

有什么想法可以从这里开始吗?

【问题讨论】:

  • 我可以使用 $("body").css("margin", "50px");和 $("body").attr("test", "hello");看来我现在无法设置 onbeforeunload 属性...

标签: javascript jquery google-chrome onbeforeunload


【解决方案1】:

您不能通过返回 false 来中止页面卸载。您必须返回将在消息框中显示给用户的字符串,并且他决定是否要离开或留在页面上(通过选择“确定”或“取消”按钮),因此您需要编写代码这个:

 window.onbeforeunload = function() {
    return "Are you sure you want to leave this page bla bla bla?"; // you can make this dynamic, ofcourse...
 };

【讨论】:

    【解决方案2】:
    window.onbeforeunload = function () { return 'Are you sure?' };
    

    【讨论】:

      【解决方案3】:

      试试这个

        <script type=\"text/javascript\">
              var dont_confirm_leave = 0; //set dont_confirm_leave to 1 when you want the user to be able to leave withou confirmation
              var leave_message = 'You sure you want to leave?'
              function goodbye(e) 
              {
                  if(dont_confirm_leave!==1)
                  {
                      if(!e) e = window.event;
                      //e.cancelBubble is supported by IE - this will kill the bubbling process.
                      e.cancelBubble = true;
                      e.returnValue = leave_message;
                      //e.stopPropagation works in Firefox.
                      if (e.stopPropagation) 
                      {
                          e.stopPropagation();
                          e.preventDefault();
                      }
      
                      //return works for Chrome and Safari
                      return leave_message;
                  }
              }   
          window.onbeforeunload=goodbye;
          </script>
      

      【讨论】:

      • 天啊天啊!你拯救了我的一天。 krcko 的假设似乎是错误的:“您不能通过返回 false 来中止页面卸载”。或者他不够精确。看起来如果“onbeforeload”事件返回 false,则(对于 IE only)不会显示任何内容。因此,我正在处理的代码返回 false,并且 IE 没有消息,但总是显示“false”的消息,并要求离开。您的解决方案适用于所有浏览器。 (Thank you)³².
      【解决方案4】:

      检查此代码:

      var validNavigation = false;
      
      function wireUpEvents() {
      var dont_confirm_leave = 0; 
      var leave_message = "You sure you want to leave ?";
      
      function goodbye(e) {
      if (!validNavigation) {
      if (dont_confirm_leave !== 1) {
      if (!e) e = window.event;
      e.cancelBubble = true;
      e.returnValue = leave_message;
      //e.stopPropagation works in Firefox.
      if (e.stopPropagation) {
      e.stopPropagation();
      e.preventDefault();
      }
      //return works for Chrome and Safari
      return leave_message;
      }
      }
      }
      
      window.onbeforeunload = goodbye;
      
      document.onkeydown = function () {
      switch (event.keyCode || e.which) {
      case 116 : //F5 button
      validNavigation = true;
      case 114 : //F5 button
      validNavigation = true;
      case 82 : //R button
      if (event.ctrlKey) {
      validNavigation = true;
      }
      case 13 : //Press enter
      validNavigation = true;
      }
      
      }
      // Attach the event click for all links in the page
      $("a").bind("click", function () {
      validNavigation = true;
      });
      
      // Attach the event submit for all forms in the page
      $("form").bind("submit", function () {
      validNavigation = true;
      });
      
      // Attach the event click for all inputs in the page
      $("input[type=submit]").bind("click", function () {
      validNavigation = true;
      });
      }
      
      // Wire up the events as soon as the DOM tree is ready
      $(document).ready(function () {
      wireUpEvents();
      });
      

      【讨论】:

        【解决方案5】:

        它并不漂亮,但它成功了。

        var warnclose = true;
        var warn = function(e) {
            var warning = 'Your warning message.';
            if (warnclose) {
                // Disables multiple calls
                warnclose = false;
        
                // In case we still need warn to be called again 
                setTimeout(function(){
                    warnclose = true;
                }, 500);
        
                return warning;
            }
        };
        window.onbeforeunload = warn;
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2011-11-02
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-08-09
          • 1970-01-01
          相关资源
          最近更新 更多