Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New to the CF scene
    Join Date
    Jan 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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!

  • #2
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,981
    Thanks
    56
    Thanked 557 Times in 554 Posts
    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.

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,014
    Thanks
    79
    Thanked 4,435 Times in 4,400 Posts
    ??? 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.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #4
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,981
    Thanks
    56
    Thanked 557 Times in 554 Posts
    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>

  • #5
    New to the CF scene
    Join Date
    Jan 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #6
    New to the CF scene
    Join Date
    Jan 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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>

  • #7
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,014
    Thanks
    79
    Thanked 4,435 Times in 4,400 Posts
    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.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •