【问题标题】:How does the "Changes you have made may not be saved?" popup show up in browsers?“您所做的更改可能无法保存”怎么办?弹出窗口出现在浏览器中?
【发布时间】:2021-06-25 15:09:47
【问题描述】:

我想要“您所做的更改可能无法保存?”当用户尝试在未正确退出的情况下关闭选项卡时弹出弹出窗口,我如何确保它显示出来?

【问题讨论】:

  • 您不应阻止用户关闭标签页。

标签: javascript google-chrome chromium


【解决方案1】:

在这里查看:Confirmation before closing of tab/browser

您可以在window.onbeforeunload 事件中检查一些内容。

编辑:

我将 yaya 的解决方案复制并粘贴到我的 html 中,如果您确定要离开,它至少会使用浏览器的默认问题。完整的sn-p:

<!DOCTYPE html>
<html>

<head>
    <title>Page Title</title>
</head>

<body>
    <h1>This is a Heading</h1>
    <p>This is a paragraph.</p>
    <script>
        var ask = true
        window.onbeforeunload = function (e) {
            if (!ask) return null
            e = e || window.event;
            //old browsers
            if (e) { e.returnValue = 'Sure?'; }
            //safari, chrome(chrome ignores text) 
            return 'Sure?';
        };
    </script>
</body>

</html>

【讨论】:

    猜你喜欢
    • 2017-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-25
    • 1970-01-01
    • 1970-01-01
    • 2013-02-20
    • 1970-01-01
    相关资源
    最近更新 更多