我想在瀏覽器關閉或瀏覽器選項卡關閉時提醒框。我有一些代碼,但在這1額外的消息即將到來..我想知道它從哪裏進入警戒框。如果有任何其他代碼,然後發送給我或告訴我該怎麼辦..其實我想避免第二次用戶登錄(爲此,我創建1會話ID並保存在數據庫後,每當用戶登錄時檢查會話ID是在數據庫或不,如果然後用戶可以登錄,當用戶註銷會話ID也被刪除,但瀏覽器或選項卡已關閉,然後會話ID不會被刪除爲此,我想檢測一個之後,我想發送控制ajax控制器)這就是爲什麼我想知道從哪裏來的額外信息。請解決我的問題..如何檢測瀏覽器/選項卡已關閉
<script type="text/javascript" >
var validNavigation = false;
function wireUpEvents() {
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 (!validNavigation) {
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;
// Attach the event keypress to exclude the F5 refresh
$('document').bind('keypress', function(e) {
if (e.keyCode == 116){
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();
});
</script>
看到這個[鏈接](http://eureka.ykyuen.info/2011/02/22/jquery-javascript-capture-the-browser-or-tab-關閉事件/) – 2013-04-01 07:33:26
我想你可以使用onunload事件。 –
如果我使用onload事件,那麼每次它發出警報框 – Anurag