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 05-20-2009, 01:33 PM   PM User | #1
urbanbutterfly
New Coder

 
Join Date: Aug 2008
Location: Essex, UK
Posts: 39
Thanks: 7
Thanked 0 Times in 0 Posts
urbanbutterfly is an unknown quantity at this point
Prompt to save dirtied form

Afternoon guys, wonder if I can get some assistance with some JavaScript I need, unfortunately my worst language.

I have a large form that needs to prompt the user to save before leaving the page, if they have edited any of the fields inside it.

Here is what I have thus far, and would like to keep it in the same format if possible:

Code:
function do_unload(){

if() {
    confirm('Changes have been made to this page. Do you wish to save them?');
    document.forms.frmlog.pAction.value='SAVE';
    document.forms.frmlog.submit();
    }
}
Code:
<body onUnload="do_unload()">

</body>
Basically, I just want to determine what inputs and selects have been changed since the page loaded, and have the logic for this in the IF statement, if that makes sense.

Thanks in advance.

Last edited by urbanbutterfly; 05-22-2009 at 09:38 AM..
urbanbutterfly is offline   Reply With Quote
Old 05-20-2009, 06:13 PM   PM User | #2
adios
Senior Coder

 
Join Date: Jun 2002
Posts: 1,404
Thanks: 2
Thanked 32 Times in 32 Posts
adios is on a distinguished road
Think this works ...

Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>untitled</title>
<script type="text/javascript">

function form_is_dirty(oForm)
{
   var el, opt, hasDefault, i = 0, j;
   while (el = oForm.elements[i++]) 
   {
      switch (el.getAttribute('type'))
      {
         case 'text' :
         case 'textarea' :
         case 'hidden' :
            if (!/^\s*$/.test(el.value) && el.value != el.defaultValue)
            {
               return true;
            }
            break;
         case 'checkbox' :
         case 'radio' :
            if (el.checked != el.defaultChecked)
            {
               return true;
            }
            break;
         case 'select-one' :
         case 'select-multiple' :
            j = 0, hasDefault = false;
            while (opt = el.options[j++])
            {
               if (opt.defaultSelected)
               {
                  hasDefault = true;
               }
            }
            j = hasDefault ? 0 : 1;
            while (opt = el.options[j++])
            {
               if (opt.selected != opt.defaultSelected)
               {
                  return true;
               }
            }
            break;
         default :
            break;
      }
   }
   return false;
}

window.onunload = function()
{
   var f = document.getElementById('frmlog');
   if (form_is_dirty(f))
   {
      if (confirm('Changes have been made to this page. Do you wish to save them?\n\n    [OK] ---> save\n    [Cancel] ---> proceed'))
      {
         f.pAction.value = 'SAVE';
         f.submit();
      }
   }
}

</script>
</head>
<body>
<a href="http://www.google.com">test link</a>
<form id="frmlog" name="frmlog" action="javascript:alert('submitted')">
<input type="text" />
<textarea rows="3" cols="8">feh!</textarea>
<input type="hidden" value="foo" />
<input type="checkbox" checked="checked" />
<input type="checkbox" />
<input type="radio" name="r1" checked="checked" />
<input type="radio" name="r1" />
<select>
<option value="1">blah 1</option>
<option value="2">blah 2</option>
<option value="3">blah 3</option>
</select>
<select multiple="multiple">
<option value="4">blah 1</option>
<option value="5" selected="selected">blah 2</option>
<option value="6" selected="selected">blah 3</option>
</select>
<br /><br /><br />
<input type="reset" value="RESET" onclick="return confirm('Reset all fields?')" />
<input type="submit" value="SUBMIT" />
<input type="text" name="pAction" />
</form>
</body>
</html>
adios is offline   Reply With Quote
Users who have thanked adios for this post:
urbanbutterfly (05-21-2009)
Old 05-21-2009, 10:12 AM   PM User | #3
urbanbutterfly
New Coder

 
Join Date: Aug 2008
Location: Essex, UK
Posts: 39
Thanks: 7
Thanked 0 Times in 0 Posts
urbanbutterfly is an unknown quantity at this point
Works wonderfully, thank you very much.
urbanbutterfly is offline   Reply With Quote
Old 05-21-2009, 03:25 PM   PM User | #4
urbanbutterfly
New Coder

 
Join Date: Aug 2008
Location: Essex, UK
Posts: 39
Thanks: 7
Thanked 0 Times in 0 Posts
urbanbutterfly is an unknown quantity at this point
adios: Apologies, one more question.

Is there a way upon saving to direct the user to the click they was originally leaving the form for?

I'm guessing a use of window.location but I'm not sure.

Kind regards
urbanbutterfly is offline   Reply With Quote
Old 05-21-2009, 05:37 PM   PM User | #5
adios
Senior Coder

 
Join Date: Jun 2002
Posts: 1,404
Thanks: 2
Thanked 32 Times in 32 Posts
adios is on a distinguished road
Might try submitting to a hidden iframe. This permits submission without serving an 'action' page (to the main window, anyway).

See if this helps.
adios is offline   Reply With Quote
Old 05-22-2009, 09:38 AM   PM User | #6
urbanbutterfly
New Coder

 
Join Date: Aug 2008
Location: Essex, UK
Posts: 39
Thanks: 7
Thanked 0 Times in 0 Posts
urbanbutterfly is an unknown quantity at this point
Thanks adios. This is a "wish-list" feature, and will be looked into when I have more time.

Many thanks for your help.
urbanbutterfly is offline   Reply With Quote
Old 11-25-2009, 06:43 AM   PM User | #7
sainath_nair
New to the CF scene

 
Join Date: Nov 2009
Posts: 4
Thanks: 1
Thanked 0 Times in 0 Posts
sainath_nair is an unknown quantity at this point
Thumbs up Thank you so much Adios...

Thank you so much Adios....I had been searching about this for hours and then when I bumped in to this forum , I saw your post and believe me, it worked for me without any modifications.

Once again thanks.

Sai@qseap
sainath_nair is offline   Reply With Quote
Old 11-25-2009, 06:51 AM   PM User | #8
adios
Senior Coder

 
Join Date: Jun 2002
Posts: 1,404
Thanks: 2
Thanked 32 Times in 32 Posts
adios is on a distinguished road
Been years ... glad that helped !

cheers, adios
adios is offline   Reply With Quote
Users who have thanked adios for this post:
sainath_nair (11-25-2009)
Old 11-25-2009, 09:41 AM   PM User | #9
sainath_nair
New to the CF scene

 
Join Date: Nov 2009
Posts: 4
Thanks: 1
Thanked 0 Times in 0 Posts
sainath_nair is an unknown quantity at this point
Quote:
Originally Posted by adios View Post
Been years ... glad that helped !

cheers, adios

yeah ....but yet no change to the code....It works perfectly.....genius indeed
sainath_nair is offline   Reply With Quote
Old 11-25-2009, 09:45 AM   PM User | #10
sainath_nair
New to the CF scene

 
Join Date: Nov 2009
Posts: 4
Thanks: 1
Thanked 0 Times in 0 Posts
sainath_nair is an unknown quantity at this point
Hey just another related issue ....

How to modify this event on tab change?? FYI, I am using richfaces tabs and it has an attribute 'ontableave' where we need to insert the js code.. Tried the same code but doesn't work....
Let me know if ur genius mind has some solution for this!
sainath_nair 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 08:42 PM.


Advertisement
Log in to turn off these ads.