CodingForums.com

CodingForums.com (http://www.codingforums.com/index.php)
-   JavaScript programming (http://www.codingforums.com/forumdisplay.php?f=2)
-   -   Ask for confirmation if user leaves the page but not if by submitting the form (http://www.codingforums.com/showthread.php?t=285212)

Fedy 01-03-2013 06:29 PM

Ask for confirmation if user leaves the page but not if by submitting the form
 
Hi everyone, this is my first post here! As I wrote in the title of the post, I need my JavaScript code to do this action: a confirmation prompt must appear when users leave a page with a form containing unsaved editings, but the prompt shouldn't appear if users submit this form clicking on some buttons.

This is the code I tried:

Code:

                var clicked;
                $(document).ready(function() { 
               
                $(".button").click(function(){
                        clicked = 1;
                });
               
                if (form_is_modified(document.forms[1])) {
                        get();
                }
                else {
                       
                }

                function get(){
                        if(clicked == 1) {
                               
                        } else {
                                confirmUnload(true);
                        }
                }
               
                function confirmUnload(on) {
                        var message = "You have unsaved data. Are you sure to leave the page?";
                        window.onbeforeunload = (on) ? function() { return message; } : null;
                }

I do not post the function "form_is_modified" through which I check if the form contains unsaved editings, because is working well. Everyone has suggestions?

Thanks so much, bye!

xelawho 01-03-2013 06:52 PM

being that you appear to be using jQuery anyway you can use unload() which gives you access to the event object. From there you could find out if the element that triggered the event was your submit button and if it wasn't pop the confirm box up.

completely untested, and there may well be a better way, but that's what occurs to me.

Old Pedant 01-04-2013 02:09 AM

??? How does jQuery know that a submit button triggered the unload?

I would think that the event would be tied to the <form>, not to the button.

xelawho 01-04-2013 04:02 AM

you're right - dumb idea. How about:

Code:

<script>
$(document).ready(function () {
    var warn=true;
    $("form").submit(function () {
        warn=false;
    });
    window.onbeforeunload = function () {
        if (warn) {
            return "sure about that?";
        }
    }
});

</script>


Fedy 01-04-2013 02:14 PM

Hi everyone and thanks for your kind replies! As soon as I can (next week) I will try the solution suggested and I will give you a feedback.

Fedy 01-08-2013 08:16 AM

Hi xelawho, your suggestion worked nicely! I just slightly changed the form in order to have the confirmation only if user changes something in the form:

Code:

<script>
$(document).ready(function () {
    var warn=true;
    $("form").submit(function () {
        warn=false;
    });
    $("form").change(function () {
        window.onbeforeunload = function () {
            if (warn) {
                return "sure about that?";
            }
        }
    });
});

</script>


Old Pedant 01-08-2013 05:53 PM

Yes. This is the correct way. Why did you ask about that other way in the other thread?

As I said in the other thread, *MAYBE* you could make a SJAX call (like AJAX, but synchronous) to save the <form> contents, but it would have to be unconditional: You would always do the save if warn is true.


All times are GMT +1. The time now is 11:06 AM.

Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.