Display Confirmation Popup before leaving page

 
<html>
<head>
    <script src="https://code.jquery.com/jquery-2.2.4.min.js"></script>
</head>
<body>

    <script>
    $(function() {
        /*
        The "changed" variable must be set to true when page has been edited/updated,
        and it must be set to false once the changes have been saved.
        */
        var changed = true;
        
        $(window).on("beforeunload", function (e) {
            if (changed) {
                e.preventDefault();
                e.returnValue = '';
            }
        });
    });
    </script>
</body>
</html>

 

Related Snippets

•  jQuery Event Delegation