我想在浏览器关闭或浏览器选项卡关闭时提醒框。我有一些代码,但在这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