"Changes you have made may not be saved?" 弹出窗口如何在浏览器中显示?

How does the "Changes you have made may not be saved?" popup show up in browsers?

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

在此处查看:Confirmation before closing of tab/browser

您可能会在 window.onbeforeunload 活动中查看一些内容。

编辑:

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

<!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>