Hi all, we are writing a cross browser Javascript control that enhances the HTML text input element in order to accept only digits (more or less). Supposing that ''domText'' is the DOM element corresponding to the HTML text input, we wrote the following: Event.observe(domText, "keypress", this.onKeyPress.bindAsEventListener(this)); where this.onKeyPress = function(evt) { var evtc = evt.keyCode; // char code for IE and Opera if (evtc == 0 || evtc == null) { // Firefox evtc = evt.charCode; } if (evtc >= 48 && evtc <= 57) { // Digit return true; } else { Event.stop(evt); } } This code works fine with IE and Firefox, but doesn''t work with Opera, for which the event is not actually stopped. Moreover, when we tried to intercept the ''+'' character, we noticed that IE did return the key code (as if the keydown event was fired) instead of the char code, as expected for the keypres event. By looking at the code of the prototype library, we noticed that actually the keydown event is fired, which is not axactly what we expect when we intercept the keypress event ;-) We don''t know if it is a bug, but it seems to us that the test if (name == ''keypress'' && (Prototype.Browser.WebKit || element.attachEvent)) name = ''keydown''; is not working properly. Anyway, by commenting the test we only fixed the problem with IE. In order to fix the event handling with Opera, we had to avoid the bindAsEventListener call: Event.observe(domText, "keypress", this.onKeyPress); Is it a problem of the prototype library or are we doing something wrong? Thank you very much. Regards Mirko Zanotti Davide Lorenzi --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Mirko wrote:> ... > Event.observe(domText, "keypress", > this.onKeyPress.bindAsEventListener(this)); > > ... > if (name == ''keypress'' && > (Prototype.Browser.WebKit || element.attachEvent)) > name = ''keydown''; > > is not working properly. Anyway, by commenting the test we only fixed > the problem with IE. In order to fix the event handling with Opera, we > had to avoid the bindAsEventListener call: > > Event.observe(domText, "keypress", this.onKeyPress); > > Is it a problem of the prototype library or are we doing something > wrong? > > ...A few things. 1. Keep in mind that a keypress is a keydown plus a keyup. So keydown works well most of the time, BUT keydown cannot be stopped through Event.stop() (see http://www.quirksmode.org/js/events_compinfo.html#keys). So the line where Prototype changes a keypress to a keydown may hinder your goal. I don''t know why it would apply to Opera or why it needs to be there at all. 2. There are a lot of quirks for cross-browser key-stroke detection (see http://www.quirksmode.org/js/keys.html). One example is the plus character. 3. It is a lot more user friendly and simpler to attach an onblur event that simply removes disallowed characters. For example: Event.observe(domText, ''blur'', function() { if (/\D/.test(domText.value)) { domText.value = domText.value.replace(/\D/g, ''''); alert(''Only numbers are allowed''); } }); - kds --~--~---------~--~----~------------~-------~--~----~ 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 Ken, thank you for your reply. For what I know, what you say is not completely exact. The keypress event differs from the keydown for the fact that, for IE, the property keyCode of the event contains the char code associated to the key pressed, instead of the key code. Firefox returns 0 in keyCode property and the char code in the charCode property. So, if the keydown event is fired, the value of the keyCode property is not what I expected. For what concerns the onblur event, unfortunately one of our constraint is that the test must be performed on the key pressed, not on focus lost (sorry, I forgot to write it in the post). Thank you again Kind regards Mirko --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
If you''re trying to differentiate between "keypress" and "keydown", you may need to be aware of these lines in Event.observe: if (name == ''keypress'' && (Prototype.Browser.WebKit || element.attachEvent)) name = ''keydown''; TAG On Jul 16, 2007, at 9:46 AM, Mirko wrote:> > Hi Ken, > > thank you for your reply. For what I know, what you say is not > completely exact. The keypress event differs from the keydown for the > fact that, for IE, the property keyCode of the event contains the char > code associated to the key pressed, instead of the key code. Firefox > returns 0 in keyCode property and the char code in the charCode > property. So, if the keydown event is fired, the value of the keyCode > property is not what I expected. > > For what concerns the onblur event, unfortunately one of our > constraint is that the test must be performed on the key pressed, not > on focus lost (sorry, I forgot to write it in the post). > > Thank you again > > Kind regards > > Mirko > > > >--~--~---------~--~----~------------~-------~--~----~ 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 Tom, this is exactly what I wrote in my first post. I don''t know why that test, but it seems that with prototype one can''t handle directly the keypress event. Moreover, I can''t understand why Event.stop() does not work with Opera if the handler is registered via bindAsEventListener. Is it a bug? Thank you Mirko --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---