window.isCloseHint = true;
//初始化封闭
window.addEventListener("beforeunload", function(e) {
if (window.isCloseHint) {
var confirmationMessage = "要记得保留!你确定要脱离我吗?";
(e || window.event).returnValue = confirmationMessage; // 兼容 Gecko + IE
return confirmationMessage; // 兼容 Gecko + Webkit, Safari, Chrome
}
});
假如你页面中有须要跳转而不像提醒对话框的处所(比方form表单提交),能够经由过程window.isCloseHint 变量来掌握。