On Nov 13, 3:30 am, ditman <[EMAIL PROTECTED]> wrote:
> Well, I'll reply to myself, to see if anybody can help me debug this
> code:
>
> I've come up with this:
>
> <script type="text/javascript">
>         // Definimos la función de hacer cosas...
>         var FormWatch = Class.create();
>         FormWatch.prototype = {
>                 initialize : function(form, options) {
>                         this.changed = false;
>                         this.form = $(form);
>
>                         // We monitor form changes...
>                         new Form.EventObserver(form, 
> this.setChanged.bind(this) );
>                         // Let's hook main window...
>                         Event.observe(window, 'beforeunload', 
> this.confirmExit.bind(this),
> false);
>                         // And let's hook submit action :) ...
>
> Event.observe(this.form,'submit',this.unsetChanged.bind(this),false);
>                 },
>                 unsetChanged : function(ev) {
>                         this.changed = false;
>                 },
>                 confirmExit : function(ev) {
>                         if (this.changed) {
>                                 Event.stop(ev);
>                         }
>                 },
>                 setChanged : function (element, value) {
>                         this.changed = true;
>                 }
>         }
> </script>
>
> And right after the form:
>
> <script type="text/javascript">
>      new FormWatch('watch_form_1');
> </script>
>
> That frankenstein mostly works

What do you mean by "mostly works"?  I can see that the initialize and
confirmExit methods are called, but that's it.  I am not prevented
from leaving a modified page at all.  I don't see how you determine
whether the form has actually changed or not.

I wrote one of these some time ago, it seemed simple to have an
onbeforeunload (OBU) handler that just checked to see if a form had
been modified and do stuff, but it doesn't work.  By the time the OBU
hanlder has decided whether or not to interupt navigation, it's too
late.  So the OBU must stop navigation always, the trick is to add or
remove it based on whether a form has been modified or not.

The toggle OBU handler runs over any form (you can use a class to
decide which should have this behaviour and which shouldn't) and at
the first instance of a modified control, sets the OBU hanlder.  If no
changes are found, it removes the OBU hanlder.  It also runs when
reset is clicked after a short pause to let the form update.

It unconditionally removes the handler if submit occurs.

1. onload:
    - add a toggle OBU handler to the form's onkeyup event
    - add a remove OBU handler to the form's submit event
    - add remove OBU handler with a pause to the form's reset event
    - add a toggle OBU handler to the onclick attribute of checkbox
and
      radio button controls (because IE's onchange handler doesn't
fire
      until such controls lose focus)
    - add a toggle OBU handler to the onchange attribute of all
      other form controls

2. Any keystroke in the form runs toggleOBU, which:
    - runs formsChanged to see if any form has been changed
    - if a form has been modified, adds a function to
window.onbeforeunload
      if there isn't one there already
    - if no form has been modified, removes window.onbeforeunload

3. Clicking submit removes the onbeforeunload immediately so the
   form is submitted with no questions asked, even if another form
   in the page has been modified

4. Clicking reset removes the onbeforeunload after a brief (5ms) pause
   only if no other form in the page has been modified-presuming
   that the 5ms pause allows the form to be reset before the check
runs

5. Any navigation away from the page-clicking a link, back, forward,
   home, close window, reload, etc. will fire onbeforeunload if it's
set

6. Can also add toggleOBU to window.load so that if a form is loaded
   into the page in a modified state, e.g. via forward or back button
   (which shouldn't be an issue anyway) onbeforeunload handler
   is still attached.

7. Note that the page can have as many forms as you like and only
   those with a class of 'checkNavigation' will cancel navigation, so
   things like search forms can be excluded from this behaviour

Known bug:
If a user right-clicks in a text area then clicks on a link outside
the page, they will navigate away as the onchange handler doesn't fire
to add the OBU handler.


>, but when I try to exit a page that has
> changed, Event.stop(ev) gets called and a generic IE/Firefox window
> pops up asking something like:

I don't see any window at all.


--
Rob


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Spinoffs" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-spinoffs?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to