在Chrome和IE中使用jQuery在body元素上设置onbeforeunload

问题描述:

我有一个系统,我想检查用户,如果他们确定他们想要在设置脏标志后离开页面。在Chrome和IE中使用jQuery在body元素上设置onbeforeunload

我正在使用下面的代码 - 在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)'); 
    } 
} 

任何想法如何从这里开始?

+0

我能够使用 $( “身体”)的CSS( “保证金”, “50像素”)。 和 $(“body”)。attr(“test”,“hello”); 看来我现在无法设置onbeforeunload属性... – Graeme 2009-11-26 10:49:55

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

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

window.onbeforeunload = function() { return 'Are you sure?' }; 

试试这个

<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> 
+0

Omg omg!你拯救了我的一天。看起来krcko的假设是错误的:“你不能通过返回false来中止页面卸载”。或者他不够精确。它看起来(只有IE **),如果“onbeforeload”事件返回false,则不显示任何内容。所以我正在处理的代码是返回false,IE没有消息,但**总是**显示“false”的消息,并要求离开。您的解决方案适用于所有浏览器。 **'(谢谢)''。 – 2013-06-26 08:44:47

+0

非常好的解决方案,非常感谢 – Dorgham 2013-11-27 12:34:24

检查这个代码:

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(); 
}); 

这些脚本WOR k很好,但是如果我想在用户确认窗口关闭时触发AJAX调用呢?

我试过,但AJAX调用被激发,如果双方用户点击“是,关闭”或“没有,住在这里”(试图在Firefox 38.x)

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(); 
      } 

      $.ajax({ 
       async: false, 
       url: "/launch-my-script.php" 
      }); 

      //return works for Chrome and Safari 
      return leave_message; 
     } 
    } 
} 
+0

请确定事件类型,这应该是新问题,而不是回答? – MarmiK 2015-08-20 09:47:55

这不是很漂亮,但它做到了。

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;