onbeforeunload事件兼容性操作

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
</head>
<script>
    // firefox 47.0.1 不兼容
    window.onbeforeunload = function (e) {
        e = e || window.event;
        // 兼容IE8和Firefox 4之前的版本
        if (e) {
            e.returnValue = '关闭提示';
//            e.returnValue = false;
        }
        // Chrome, Safari, Firefox 4+, Opera 12+ , IE 9+
        return '关闭提示';
//        return false;
    };

    // firefox 47.0.1 不兼容
/*    window.onbeforeunload = function () {
        if ( /Firefox[/s](d+)/.test(navigator.userAgent) && new Number(RegExp.$1) >= 4) {
            if(confirm('您的内容尚未保存,确定要离开本页吗?')){
                history.go();
            } else{
                window.setTimeout(function() { window.stop(); }, 1);
            }
        } else{
            return '您的内容尚未保存,确定要离开本页吗?';
        }
    };*/

</script>
<body>
<h1>Hello World!</h1>
</body>
</html>

原文地址:https://www.cnblogs.com/archermeng/p/7537321.html