js阻挠页面封闭准确姿态

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 变量来掌握。

    原文作者:xbynet
    原文地址: https://segmentfault.com/a/1190000007742030
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞