简体   繁体   中英

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

I want the "Changes you have made may not be saved?" popup to show up when a user tries to close a tab without properly exiting, how do I make sure it shows up?

Check here: Confirmation before closing of tab/browser

You may what to check some stuff on the window.onbeforeunload event.

Edit:

I copied and pasted the solution from yaya into my html which will at lease use the browsers default question if you are sure to leave. Full snippet:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM