First things first: Hi all! I''ve been sucked into prototype and script.aculo.us develpment thanks (because of?) the Symfony PHP framework... I''d like to implement a feature on some admin pages, but I can''t seem to find the right search term to google it; I want to ask the user to confirm exit of a page, if she has changed anything on the forms displayed on it. I have some knowledge in other JS frameworks (did some things in Mootools) but for this thing I don''t know where to start. Any advice would be greatly appreciated :) Thanks!! --~--~---------~--~----~------------~-------~--~----~ 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 rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
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, 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: Are you sure you want to exit this page? false <- this only appears in IE Click OK to continue, or Cancel to stay in the current page. That''s exactly what I want to achieve, but I wouldn''t mind to be able to control that popup (and get rid of that "false" in IE)... Help please! I''m almost there!! :) --~--~---------~--~----~------------~-------~--~----~ 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 rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
Andrew Kaspick
2007-Nov-13 02:56 UTC
Re: If form changed ask if you really want to exit page...
This is the basics of what I use now... function check_form_data_for_unload(event_obj) { var form_data_message = "You have unsaved data."; if (window.form_changed) { if (event_obj) event_obj.returnValue = form_data_message; else return form_data_message; } } window.form_changed = false; window.onbeforeunload = check_form_data_for_unload; If I change a form value, I simply set form_changed to true. I do a bit more than what I show here, but this should be good enough to get you started. On 11/12/07, ditman <ditman-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > First things first: Hi all! I''ve been sucked into prototype and > script.aculo.us develpment thanks (because of?) the Symfony PHP > framework... > > I''d like to implement a feature on some admin pages, but I can''t seem > to find the right search term to google it; I want to ask the user to > confirm exit of a page, if she has changed anything on the forms > displayed on it. > > I have some knowledge in other JS frameworks (did some things in > Mootools) but for this thing I don''t know where to start. > > Any advice would be greatly appreciated :) > > Thanks!! > > > > >--~--~---------~--~----~------------~-------~--~----~ 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 rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
On Nov 13, 3:30 am, ditman <dit...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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 worksWhat 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 rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
On Nov 13, 4:13 am, RobG <rg...-AFFH1GffN5hPR4JQBCEnsQ@public.gmane.org> wrote:> On Nov 13, 3:30 am, ditman <dit...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > >, 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 >Hi there! I''ve tested that script in IE / Firefox, and it does show a window. Try this html: <form id="watch_me"> <input type="text" id="thing1" name="thing1" value="" /><br /> <input type="text" id="thing2" name="thing2" value="" /><br /> <input type="text" id="thing3" name="thing3" value="" /> <input type="submit" value=" SUBMIT " /> </form> <script type="text/javascript"> new FormWatch(''watch_me''); </script>> 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.If the form has changed, the script calls Event.stop(ev); Which I thought it was enough to "stop" the event from happening... That''s the line that pops up a standard window (the same that will pop up if you click here in a link while composing a reply, but instead of saying "Your reply message has not been sent", it says "null" in IE, and nothing at all in Firefox).> I don''t see how you determine whether the form has actually changed or not.new Form.EventObserver(form, this.setChanged.bind(this) ); I think that line does the trick... --~--~---------~--~----~------------~-------~--~----~ 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 rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
Well, apparently my version doesn''t work in Opera... I''ll try those steps Rob, to see if I can get this working... On Nov 13, 8:29 am, ditman <dit...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Nov 13, 4:13 am, RobG <rg...-AFFH1GffN5hPR4JQBCEnsQ@public.gmane.org> wrote: > > > On Nov 13, 3:30 am, ditman <dit...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > >, 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 > > Hi there! > > I''ve tested that script in IE / Firefox, and it does show a window. > > Try this html: > > <form id="watch_me"> > <input type="text" id="thing1" name="thing1" value="" /><br /> > <input type="text" id="thing2" name="thing2" value="" /><br /> > <input type="text" id="thing3" name="thing3" value="" /> > <input type="submit" value=" SUBMIT " /> > </form> > <script type="text/javascript"> > new FormWatch(''watch_me''); > </script> > > > 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. > > If the form has changed, the script calls Event.stop(ev); Which I > thought it was enough to "stop" the event from happening... That''s the > line that pops up a standard window (the same that will pop up if you > click here in a link while composing a reply, but instead of saying > "Your reply message has not been sent", it says "null" in IE, and > nothing at all in Firefox). > > > I don''t see how you determine whether the form has actually changed or not. > > new Form.EventObserver(form, this.setChanged.bind(this) ); I think > that line does the trick...--~--~---------~--~----~------------~-------~--~----~ 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 rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
Matt Foster
2007-Nov-13 14:35 UTC
Re: If form changed ask if you really want to exit page...
First things first right? So we need to attach a change event handler to the form, unfortunately in my experiences IE did not play nice with this and did not catch the bubbling event of an input''s change event by observing the form, it does in FF and that way makes sense but I''ve had to iterate over all of the form.elements and attach the listener explicitly. $A(form.elements).each(function(element){ Event.observe(element, "change", someFunc); }): At some point you get into handling the boolean in the event handler but that is something to handle within your class, good luck you''ve almost got it. On Nov 13, 2:33 am, ditman <dit...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Well, apparently my version doesn''t work in Opera... > > I''ll try those steps Rob, to see if I can get this working... > > On Nov 13, 8:29 am, ditman <dit...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > On Nov 13, 4:13 am, RobG <rg...-AFFH1GffN5hPR4JQBCEnsQ@public.gmane.org> wrote: > > > > On Nov 13, 3:30 am, ditman <dit...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > >, 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 > > > Hi there! > > > I''ve tested that script in IE / Firefox, and it does show a window. > > > Try this html: > > > <form id="watch_me"> > > <input type="text" id="thing1" name="thing1" value="" /><br /> > > <input type="text" id="thing2" name="thing2" value="" /><br /> > > <input type="text" id="thing3" name="thing3" value="" /> > > <input type="submit" value=" SUBMIT " /> > > </form> > > <script type="text/javascript"> > > new FormWatch(''watch_me''); > > </script> > > > > 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. > > > If the form has changed, the script calls Event.stop(ev); Which I > > thought it was enough to "stop" the event from happening... That''s the > > line that pops up a standard window (the same that will pop up if you > > click here in a link while composing a reply, but instead of saying > > "Your reply message has not been sent", it says "null" in IE, and > > nothing at all in Firefox). > > > > I don''t see how you determine whether the form has actually changed or not. > > > new Form.EventObserver(form, this.setChanged.bind(this) ); I think > > that line does the trick...--~--~---------~--~----~------------~-------~--~----~ 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 rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
Walter Lee Davis
2007-Nov-13 14:53 UTC
Re: If form changed ask if you really want to exit page...
On Nov 13, 2007, at 9:35 AM, Matt Foster wrote:> > First things first right? > > So we need to attach a change event handler to the form, unfortunately > in my experiences IE did not play nice with this and did not catch the > bubbling event of an input''s change event by observing the form, it > does in FF and that way makes sense but I''ve had to iterate over all > of the form.elements and attach the listener explicitly.What if you were to serialize the form at page load, then again at page before leave, and compare the two serialized values to note any changes? Wouldn''t that shortcut this whole thing? Walter --~--~---------~--~----~------------~-------~--~----~ 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 rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
On Nov 13, 5:29 pm, ditman <dit...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Nov 13, 4:13 am, RobG <rg...-AFFH1GffN5hPR4JQBCEnsQ@public.gmane.org> wrote: > > > On Nov 13, 3:30 am, ditman <dit...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > >, 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 > > Hi there! > > I''ve tested that script in IE / Firefox, and it does show a window. > > Try this html: > > <form id="watch_me"> > <input type="text" id="thing1" name="thing1" value="" /><br /> > <input type="text" id="thing2" name="thing2" value="" /><br /> > <input type="text" id="thing3" name="thing3" value="" /> > <input type="submit" value=" SUBMIT " /> > </form> > <script type="text/javascript"> > new FormWatch(''watch_me''); > </script> > > > 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. > > If the form has changed,Not exactly. Form.EventObserver attaches your function to the change event of some form controls and the click event of checkboxes and radio buttons (because of how IE handles the change event for these elements I''d guess). So if you change the form or click any checkbox or radio, then set the form back to its unchanged state (say using reset or just changing it back) your function thinks the form has changed. It seems problematic whether it works when the page has been reloaded or radio buttons are involved.> the script calls Event.stop(ev); Which I > thought it was enough to "stop" the event from happening...Returning false from the prompt is sufficient. I can''t see that a beforeunload event on the window has anywhere to propagate to. :-)> That''s the > line that pops up a standard window (the same that will pop up if you > click here in a link while composing a reply, but instead of saying > "Your reply message has not been sent", it says "null" in IE, and > nothing at all in Firefox). > > > I don''t see how you determine whether the form has actually changed or not. > > new Form.EventObserver(form, this.setChanged.bind(this) ); I think > that line does the trick...No, it doesn''t. It just adds the setChange function as a handler for the change and click events as noted above. It doesn''t actually check if the form has changed from its default. -- 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 rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
On Nov 14, 12:35 am, Matt Foster <mattfoste...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> First things first right? > > So we need to attach a change event handler to the form, unfortunately > in my experiences IE did not play nice with this and did not catch the > bubbling event of an input''s change event by observing the form,There is no onchange intrinsic event defined for form elements in the W3C HTML specification, nor is it part of the unspecified "DOM 0" stuff, so you can''t expect a browser to imlement it.> it does in FFWhich might be convenient if you are coding *only* for browsers that implement it but not otherwise.> and that way makes sense but I''ve had to iterate over all > of the form.elements and attach the listener explicitly.Presumably you are attaching a handler for the change event, which is what Form.EventObserver does (more or less).> > $A(form.elements).each(function(element){ > > Event.observe(element, "change", someFunc);Which depends on the onchange event firing before the unload event occurs, which it frequently does not. Using the change event also doesn''t help to determine whether the form has changed from its default state, it only tells you if a control has changed since it got focus. Where the use of onchange is necessary, it is more useful to use the click event for radio and checkbox controls (as does EventObserver) because of the differences between when browsers fire the change event for those elements. The bottom line is that using onchange to intercept unload simply doesn''t work reliably. -- 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 rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
Hi all, thanks for all your feedback. I''ve tried to implement 2 suggestions that you have made here... First one: observe the OBU event ONLY when changes have been made in the form (actually it''s on any event of the form, really, don''t need too much sophistication here :) ), and then stopObserving it when the form is submitted: <script type="text/javascript"> var FormWatch2 = Class.create(); FormWatch2.prototype = { initialize : function(form, options) { this.changed = false; this.form = $(form); // Observe any form event... this.form_event_observer = new Form.EventObserver(form, this.setChanged.bind(this) ); // Observe submit event... Event.observe(this.form,''submit'',this.unsetChanged.bind(this)); }, unsetChanged : function() { Event.stopObserving(window, ''beforeunload'', this.confirmExit, false); }, confirmExit : function(ev) { return Event.stop(ev); }, setChanged : function () { Event.observe(window, ''beforeunload'', this.confirmExit); } } </script> That thing works only on firefox. Not only that, if I do: ev.returnValue = "You haven''t saved your changes !!" right before return Event.stop(ev); the script behaves exactly the way I want :) I''ve also tried a third version, which is the same as above, but trying to observe every form element independently, replacing the "new Form.EventObserver..." by this: Form.getElements(this.form).each(function(element) { Event.observe(element, ''blur'', this.setChanged.bind(this), false); }); But that doesn''t work at all; I get this JS error (in Firefox, in IE it''s even more cryptic): this.setChanged has no properties And I''m not able to fix it. As for the discussion of what would be the best form watcher, I agree that comparing the elements to see if they have really changed and things like that is very nice, but I would be really really happy if I could this get working with ANY event; at least in IE6 / Firefox 2 / Opera 9 which are the browsers that I''ll be using this afterwards :) Just to monitor something like "have you interacted with the form in any way? show warning if you don''t submit", fancy stuff like serializing / comparing and all that stuff sound really nice, but I lack the prototypejs skills to implement it yet :) Thanks again for your help, I feel I''m getting closer to the solution :) --~--~---------~--~----~------------~-------~--~----~ 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 rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
Michael Peters
2007-Nov-14 14:32 UTC
Re: If form changed ask if you really want to exit page...
ditman wrote:> I''ve tried to implement 2 suggestions that you have made here...May I suggest a 3rd? When your page is loaded serialize the form into a JSON string and store the results. Then in your unload handler serialize the form again. If the 2nd serialized string matches the first then nothing has changed so you ignore it. If it has, then something has changed so you warn the user. -- Michael Peters Developer Plus Three, LP --~--~---------~--~----~------------~-------~--~----~ 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 rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
On 14 nov, 15:32, Michael Peters <mpet...-aUYv5hkjw45l57MIdRCFDg@public.gmane.org> wrote:> ditman wrote: > > I''ve tried to implement 2 suggestions that you have made here... > > May I suggest a 3rd? When your page is loaded serialize the form into a JSON > string and store the results. Then in your unload handler serialize the form > again. If the 2nd serialized string matches the first then nothing has changed > so you ignore it. If it has, then something has changed so you warn the user. >Cool solution, this simplifies my first version a lot, but still has the following problems: - Doesn''t work with Opera - I can''t customize the returnValue of the event in IE :( Here it goes: <script type="text/javascript"> var FormWatch4 = Class.create(); FormWatch4.prototype = { initialize : function(form, options) { this.form = $(form); // Lets serialize this.form and store it... this.formcontents = $(form).serialize(); // Observe beforeunload event... Event.observe(window, ''beforeunload'', this.confirmExit.bind(this)); }, confirmExit : function(ev) { this.newcontents = this.form.serialize(); if (this.formcontents != this.newcontents) { return Event.stop(ev); } } } </script> How can I fix that code to modify the returnValue of "ev" in confirmExit ? ev.returnValue = "My Error String" works in FF but not in IE. Then... How can I make it work in Opera?? :D Thanks! :) --~--~---------~--~----~------------~-------~--~----~ 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 rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
Sorry, my v4.0 didn''t check if the form was being submitted... Here is the updated code. If you test this in Firefox (windows 2), you''ll see what I want to achieve :) In IE the returnValue of the event doesn''t get sent (so in the confirmation window it just writes "false" ). In opera it doesn''t work. Even though, if I can get it to work in IE / FF, I''ll be happy :) Here''s the script v4.1 <script type="text/javascript"> var FormWatch4 = Class.create(); FormWatch4.prototype = { initialize : function(form, options) { this.submitted = false; this.form = $(form); // Let''s serialize this.form and store it... this.formcontents = $(form).serialize(); // Observe beforeunload event... Event.observe(this.form, ''submit'', function() {this.submitted true; }.bind(this)); Event.observe(window, ''beforeunload'', this.confirmExit.bind(this)); }, confirmExit : function(ev) { this.newcontents = this.form.serialize(); if ((this.formcontents != this.newcontents) && ! (this.submitted)) { ev.returnValue = "This is my error string"; return Event.stop(ev); } } } </script> --~--~---------~--~----~------------~-------~--~----~ 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 rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
David Iglesias
2007-Nov-16 07:12 UTC
Re: If form changed ask if you really want to exit page...
Well, I can''t get this to work... Any charitable soul can send me the solution? :) Thanks! :D --~--~---------~--~----~------------~-------~--~----~ 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 rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
David Iglesias
2007-Nov-19 09:15 UTC
Re: If form changed ask if you really want to exit page...
Hi all! I finally solved this ;) Removing "return Event.stop(ev);" did the trick ;) On 11/16/07, David Iglesias <ditman-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Well, I can''t get this to work... > > Any charitable soul can send me the solution? :) > > Thanks! :D >--~--~---------~--~----~------------~-------~--~----~ 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 rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---