2011-08-06 176 views
6

我需要在使用javascript或PHP關閉瀏覽器窗口之前顯示確認對話框。當我點擊瀏覽器的關閉按鈕時,確認框應該出現;否則,不應顯示該對話框。關閉瀏覽器窗口之前的javascript確認對話框

感謝

+2

po [瀏覽器關閉之前提示用戶]的可能副本(http://stackoverflow.com/questions/2923139/prompt-user-before-browser-close) – KooiInc

+3

請在嘗試提問之前嘗試搜索。這個問題有好幾次回答。在關閉之前嘗試搜索* [javascript]對話框* – KooiInc

回答

4

使用此代碼,我使用的早些時候,我here

<html> 
<head> 
<title>.:I 0wn U:.</title> 
<script language="JavaScript"> 
<!-- 
window.onbeforeunload = bunload; 

function bunload(){ 
dontleave="Are you sure you want to leave?"; 
return dontleave; 
} 
//--> 
</script> 
</head> 
<body> 
Please stay on this page! 
</body> 
</html> 
12

這將關閉瀏覽器時,它顯示:

window.onbeforeunload = function (event) { 
    var message = 'Sure you want to leave?'; 
    if (typeof event == 'undefined') { 
    event = window.event; 
    } 
    if (event) { 
    event.returnValue = message; 
    } 
    return message; 
} 
1

使用jQuery:

$(window).bind('beforeunload', function(e) { 
    // Your code and validation 
    if (confirm) { 
     return "Are you sure?"; 
    } 
});