View Single Post
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