2009-11-26 70 views
11

我有一個系統,我想檢查用戶,如果他們確定他們想要在設置髒標誌後離開頁面。在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

回答

17

您不能通過返回false來中止頁面卸載。你必須返回將在用戶消息框中顯示的字符串,然後他決定是否要離開或留在頁面上(通過選擇「確定」或「取消」按鈕),所以你需要編寫代碼這樣的:

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

試試這個

<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

1

檢查這個代碼:

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

這些腳本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

0

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

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; 
相關問題