0

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>

1 Answer 1

1

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>
Sign up to request clarification or add additional context in comments.

2 Comments

Are you sure this works because when I click the button the pop up doesnt occur
This is the window origin script: <script type="text/javascript"> window.open('popup.html'); </script>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.