简体   繁体   中英

Pop up window using HTML

When the button is clicked, I want to ask a series of messages and give the user a secondary confirmational option. However it doesn't work.

<html>
<body>
    <p>Close the page to trigger the onunload event.</p>
    <script type="text/javascript">
        var changes = false;        
        window.onbeforeunload = function() {
            if (changes)
            {
                var message = "Are you sure you want to navigate away from this page?\n\nYou have started writing or editing a post.\n\nPress OK to continue or Cancel to stay on the current page.";
                if (confirm(message)) return true;
                else return false;
            }
        }
    </script>

    <button onClick='function;'> </input>
</body>
</html>

Just add the "e" inside the function() and the variable changes.

Before: window.onbeforeunload = function() {

After: window.onbeforeunload = function(e) {

<html>
<body>
    <p>Close the page to trigger the onunload event.</p>
    <script type="text/javascript">   
        window.onbeforeunload = function(e) {            
            var message = "Are you sure you want to navigate away from this page?\n\nYou have started writing or editing a post.\n\nPress OK to continue or Cancel to stay on the current page.";
            if (confirm(message)) return true;
            else return false;
        }
    </script>

    <button onClick='window.close();'> </input>
</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