我在卸载前打开和卸载方法方面遇到了一些问题。我认为我根据onload方法设置了正确的代码,只有在onbeforeunload的confirm方法设置为true后才会启动。但遗憾的是,事实并非如此,即使onbeforeunload方法设置为false,并且用户希望留在网站上,卸载方法也会启动。这是我正在处理的代码,自从我开始希望它没问题以来,它已经改变了很多。我确信它不是,因为它没有按照我希望的方式工作。
var validNavigation = false;
function wireUpEvents() {
var leave_message = 'Leaving the page ?';
jQuery(function goodbye() {
jQuery(window).bind('onbeforeunload', function() {
setTimeout(function() {
setTimeout(function() {
jQuery(document.body).css('background-color', 'red');
}, 10000);
},1);
return leave_message;
});
});
function leave() {
if(!validNavigation) {
killSession();
}
}
//set event handlers for the onbeforeunload and onunloan events
window.onbeforeunload = goodbye;
window.onunload=leave;
}
// Wire up the events as soon as the DOM tree is ready
jQuery(document).ready(function () {
wireUpEvents();
});