Search code examples
javascriptgoogle-chromechromium

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?


Solution

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