My test case is simple: I have an input, with an onclick event. When the user clicks the input, an ajax request is sent. Simple. Works fine. Now, I want to be a lil smarter, and make it so the onclick of the input fires when the user presses some predefined keyboard key. Escape, for example (27). So I add the new code to catch the escape key (on the body), make it so the onclick of the input fires when escape is pressed. Still fairly standard. Still working, up to that point. My problem is this: When the user presses escape, the ajax fires, but in firefox (and firefox only, not on IE or safari), I get an exception, and it''s the error code that fires instead. Only when I press select. If I still click the button normally, it works fine. The tricky part: Now, I''ve found an ugly way for it to work. If, after creating my ajax request object, og my onclick function, I fire up an alert of some kind, then the ajax request works fine. Obviously, this doesn''t really fix anything. It just gives a hint as to what might be happening behind the scenes. Also, I can confirm it''s not the event firing code per se that is wrong. If I call the same code (the apply part) using another technique, then the ajax also works. So it''s really related to the key press in some way. The question: Any idea what might be happening? What I''m doing wrong? I''ve included my code below. <code> <html> <head> <title></title> <script type="text/javascript" src="js/prototype.js"></script> <script type="text/javascript"> function doit(){ alert("doin it"); var URL = "XML?listener=something"; //not really important var ajaxRequest = new Ajax.Request( URL, { onSuccess: doit2, onFailure: doit3 } ); //alert("s"); } function doit2(){ alert("doit2"); } function doit3(){ alert("doit3"); } function bodyKeyPress(event){ var k; //get the key code if(event.keyCode){ k = event.keyCode; } if(k && k == "27"){ var element = document.getElementById("go"); element["onclick"].apply(element); } } function simulate(){ //alert(''go''); var element = document.getElementById("go"); element["onclick"].apply(element); } </script> </head> <body onkeypress="bodyKeyPress(event)"> <center> <table border=''1'' style=''margin: 300 0 0 0''> <tr> <td> <input type="button" id=''go'' value="Do It" name="Do It" onclick=''doit()'' /> </td> <td> <input type="button" id=''go2'' value="Do It 2" name="Do It 2" onclick=''simulate()'' /> </td> </tr> </table> </center> </body> </html> </code> --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Oh wow that is some crazy code, I would really avoid that form of syntax of invoking an onclick event. I am surprised that actually works in any browser. If you''re working with an input element I think you could work some stuff around so you''re watching an executable event such as focus, so that you can call it at another point in the code. If you''re looking for more structure try wrapping a form around it and watching the submit event, another executable event. The ele.onclick.apply(ele) works if you''ve used the model of assigning a function value to the onclick property but this would fail completely if you used something like Event.observe. On Jun 29, 10:42 am, Frank <kal...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> My test case is simple: > > I have an input, with an onclick event. When the user clicks the > input, an ajax request is sent. Simple. Works fine. > > Now, I want to be a lil smarter, and make it so the onclick of the > input fires when the user presses some predefined keyboard key. > Escape, for example (27). So I add the new code to catch the escape > key (on the body), make it so the onclick of the input fires when > escape is pressed. Still fairly standard. Still working, up to that > point. > > My problem is this: > > When the user presses escape, the ajax fires, but in firefox (and > firefox only, not on IE or safari), I get an exception, and it''s the > error code that fires instead. Only when I press select. If I still > click the button normally, it works fine. > > The tricky part: > > Now, I''ve found an ugly way for it to work. If, after creating my ajax > request object, og my onclick function, I fire up an alert of some > kind, then the ajax request works fine. Obviously, this doesn''t really > fix anything. It just gives a hint as to what might be happening > behind the scenes. > > Also, I can confirm it''s not the event firing code per se that is > wrong. If I call the same code (the apply part) using another > technique, then the ajax also works. So it''s really related to the key > press in some way. > > The question: > > Any idea what might be happening? What I''m doing wrong? I''ve included > my code below. > > <code> > <html> > <head> > <title></title> > <script type="text/javascript" src="js/prototype.js"></script> > <script type="text/javascript"> > function doit(){ > alert("doin it"); > var URL = "XML?listener=something"; //not really > important > var ajaxRequest = new Ajax.Request( > URL, > { > onSuccess: doit2, > onFailure: doit3 > } > ); > //alert("s"); > } > > function doit2(){ > alert("doit2"); > } > > function doit3(){ > alert("doit3"); > } > > function bodyKeyPress(event){ > var k; > > //get the key code > if(event.keyCode){ > k = event.keyCode; > } > > if(k && k == "27"){ > var element = document.getElementById("go"); > element["onclick"].apply(element); > } > } > > function simulate(){ > //alert(''go''); > var element = document.getElementById("go"); > element["onclick"].apply(element); > } > </script> > </head> > <body onkeypress="bodyKeyPress(event)"> > <center> > <table border=''1'' style=''margin: 300 0 0 0''> > <tr> > <td> > <input type="button" id=''go'' value="Do It" > name="Do It" onclick=''doit()'' /> > </td> > <td> > <input type="button" id=''go2'' value="Do It 2" > name="Do It 2" onclick=''simulate()'' /> > </td> > </tr> > </table> > </center> > </body> > </html> > </code>--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Maybe I''m misunderstanding your answer, but I don''t think it''s what I was talking about. The event firing part works fine. The method (doit) is called. But the AJAX call throws an exception. The call is made to server. The server receives the request. But at some point after the server receives the request, something breaks up (only in this case, not with a classic click or when the apply is called from somewhere else), and the firefox browser throws an exception. I''ve tried it with low-level javascript calls, and it doesn''t seem to be prototype-related but browser-related (since I can repeat behavior at will with different ajax framework). All I was able to figure out for now is: 1-call to function doit works 2-ajax request sent to server 3-server receives request 4-server never sends reply, browser throws an error before that can happen Still just trying to understand what is happening. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Moment of glee: Ahhh. Got it through some reduction of my test case. Answer is pretty interesting too. Fact: On firefox, pressing escape(key code 27) cancels an (all?) ongoing ajax request. Test case: Do a request. Have it take 10 seconds (do a sleep on the server side). Make it process normally, to be sure it works. Try it again, but press cancel in the middle of the operation: this time, the request is cancelled. Comment: Tricky stuff... --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---