我想在浏览器关闭或浏览器选项卡关闭时发出警告框。我有一些代码,但在这 1 条额外消息中即将到来.. 我想知道它从哪里出现在警报框中。如果您有任何其他代码然后发送给我或告诉我应该怎么做..实际上我想避免第二个用户登录(为此我创建1个会话ID并在每次用户登录时保存在数据库中,它检查会话ID是如果然后用户可以登录,是否在 DB 中,当用户注销会话 id 也被删除但浏览器或选项卡关闭时,会话 id 不会被删除。为此,我想检测那个,然后我想将控件发送到 ajax控制器)这就是为什么我想知道 1 条额外消息来自何处。请解决我的问题..
<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>