Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-03-2013, 06:29 PM   PM User | #1
Fedy
New to the CF scene

 
Join Date: Jan 2013
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Fedy is an unknown quantity at this point
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!
Fedy is offline   Reply With Quote
Old 01-03-2013, 06:52 PM   PM User | #2
xelawho
Senior Coder

 
xelawho's Avatar
 
Join Date: Nov 2010
Posts: 2,437
Thanks: 52
Thanked 453 Times in 451 Posts
xelawho will become famous soon enoughxelawho will become famous soon enough
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.
xelawho is offline   Reply With Quote
Old 01-04-2013, 02:09 AM   PM User | #3
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,168
Thanks: 59
Thanked 3,993 Times in 3,962 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
??? 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.
Old Pedant is offline   Reply With Quote
Old 01-04-2013, 04:02 AM   PM User | #4
xelawho
Senior Coder

 
xelawho's Avatar
 
Join Date: Nov 2010
Posts: 2,437
Thanks: 52
Thanked 453 Times in 451 Posts
xelawho will become famous soon enoughxelawho will become famous soon enough
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>
xelawho is offline   Reply With Quote
Old 01-04-2013, 02:14 PM   PM User | #5
Fedy
New to the CF scene

 
Join Date: Jan 2013
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Fedy is an unknown quantity at this point
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 is offline   Reply With Quote
Old 01-08-2013, 08:16 AM   PM User | #6
Fedy
New to the CF scene

 
Join Date: Jan 2013
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Fedy is an unknown quantity at this point
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>
Fedy is offline   Reply With Quote
Old 01-08-2013, 05:53 PM   PM User | #7
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,168
Thanks: 59
Thanked 3,993 Times in 3,962 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
Old Pedant is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 10:06 PM.


Advertisement
Log in to turn off these ads.