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 变量来控制。