Faulty Firing

Friday, April 15, 2005

As geeks sometimes do, I was discussing event firing with respect to the input element with fellow members of #javascript@EFNet. I made a simple test script that shows the ordering of event firing. The inconsistencies across different browsers are interesting and funny; that is, if you’re a geek.

When considering the ordering of the events fired, I performed three basic tests on a fresh load of my test script:

  1. Click the input element.
  2. Click the label element.
  3. Click the input element then click the label element.

The different behaviors across the browsers are as follows:

Mozilla (1.7.6)

I’ll start with Mozilla first because it seems to behave the most intuitively to me.

  1. mousedown, focus, mouseup, click, change - There’s nothing special here. The focus event fires after the mousedown, mirroring the visual behavior, and after the click event comes the change event because the input’s state is changed from unchecked to checked.
  2. focus, click, change - Again, nothing strange here. Clicking the label focuses the input and fires its click event, which leads to the change event again.
  3. blur, focus, click - In this case the input is already focused, so clicking on the label makes the input blur, but then the label sets focus to the input once more and the click event is fired. This also seems intuitive to me.

Internet Explorer (6, SP2)

IE’s behavior, although mostly competent, seems a bit less intuitive.

  1. mousedown, focus, mouseup, click - Same as Mozilla, but the change event isn’t fired. This is because, for some strange reason, DOM 2 Events specifies that the change event shouldn’t fire until the input element loses focus. This seems dumb to me because I want to know that an element has changed when it changes, not after the fact. Also note that “loses focus” is not the same as blur–if the browser window loses focus, the input’s blur event will be fired, but not the change event. It’s worth noting that the click event should be used whenever you’re trying to monitor the changes of radio buttons.
  2. click, focus - Here the order is switched as the click event is fired before the focus event. I can’t see any reason for this inconsistency. The change event is, of course, still missing.
  3. change, blur, click, focus - Here again the click and focus events are strangely reversed. Update: IE6 fires a change event before the other three. This was missed in original testing.

Opera (8.0)

Opera behaves mostly the same as IE, but doesn’t do the weird inconsistent switch of focus and click. It does have a quirk of its own though. I have my same qualms with the firing of the change event, and that’s something that the W3C messed up on.

  1. focus, mousedown, click, mouseup - The focus event is fired before the mousedown. I have no idea where that one came from.
  2. focus, click
  3. blur, focus, click

Safari (1.3)

Safari really didn’t have a fair fight here because it lacks label support altogether. Hyatt, however, has assured me that he’s added support and that it will be in a future release (but sadly not with Tiger).

  1. mousedown, mouseup, click - Oops! Where’d my focus event go? Apparently that event was completely forgotten.
  2. Un-testable
  3. Un-testable

Konqueror 3.5.x

Update! Konqueror comes late in the game.

  1. mousedown, focus, mouseup, change, click - like Mozilla, but change and click are switched.
  2. focus, mouseup, change, click - mouseup is a new one for this test; change and click are still switched.
  3. blur, focus, mouseup, click - again just a mouseup that Mozilla doesn’t have.

So what have we learned from all of this? Mozilla implemented common sense behavior while sacrificing strict compliance to the lame (IMHO) spec. Kudos.

IE has some weird order switching that probably won’t matter in 99.9% of normal cases, but it’s bizarre nonetheless. Also in the process of testing I found that an input element without a name cannot be checked in IE.

Opera fires focus before mousedown, which is just…weird. Other than that it’s to spec.

Safari fumbles on the focus event and we’re waiting on label support. I would also like to applaud and thank Dave Hyatt for his transparent look into the development changes he’s making on Safari to address the problems highlighted by the new Acid2 test.

Konqueror is better than some, but the activation of the controls happens after mouseup instead of after click like the others. A KDE dev has said he might change this behavior to match the others.

All in all it’s been an interesting day.

written by Brad Fults

Add your thoughts | Trackback URL

Archived at: http://h3h.net/2005/04/faulty-firing/

8 responses

  1. jehiah

    Good comparisons. I didn’t know about a few of those quirks.

    You might also be interested to know that the onchange doesn’t fire when you fill an input field from the browsers autocomplete list in IE.

    http://jehiah.com/archive/onchange-and-autocomplete

    yick.

  2. Asheesh

    ya it is true onchange dosen’t fire when autocomplete is enable . but you can set it ‘off’ at form level as well as at control level . and if you want to disable it using javascript then you can do it . there is a lot of stuff available on the net regarding this issue.

    bye

  3. braclayrab

    They should just make onchange never fire ever, that way no programmer will ever make javascript that hurts anyone.

  4. blog.jc21 » Blog Archive » onChange vs onClick Event

    [...] In a way yes it worked - in every browser but IE. After reading a post about browser inconsistencies I learned that IE doesn’t fire the onChange event, until after I lose focus of the option I just changed - which just doesn’t make sense! [...]

  5. Keilaron

    Interesting glitches.
    What I find the most amusing, however, is that you’re giving Mozilla kudos for breaking the spec.

    Anyway, isn’t the spec that way for accessibility reasons?

  6. Brad Fults

    Yeah, I give Mozilla kudos because the behavior seems more intuitive to me.

    If the spec is the way it is because of accessibility reasons then that makes more sense. Not sure what those would be though.

  7. Keilaron

    I don’t profess to know the reason, honestly; But I seem to recall thinking that it made more sense and that there was a reason behind it.
    Well, for a little while during writing this comment I kept trying to find reasons for, or even against… and I just couldn’t. It seemed to me, someone who’s experience and knowledge on Javascript is (to be honest) fairly limited, that onChange is almost redundant anyway.
    If you want to check with every keystroke*, there’s onKeyDown. If you want to check when the input is over, there’s onBlur (or am I mistaken on this?). Okay, so onChange would make on-the-spot checks easier to do (since I’m guessing, though you may want to check this as well, that it would for example capture paste operations), but I still feel that such actions aren’t necessary.
    Oh, now I remember what it was. For select boxes it was better to fire the onChange event afterwards because keyboard selection of the proper item would otherwise be a PITA; Think of sites where selecting via the dropdown causes the page to change or submit…

    * But there’s always the mouse and other inputs…

  8. h3h.net - Event Handling with input & label Redux

    [...] is a brief follow-up to Faulty Firing (see that post for the description of the tests) with updated results in the latest [...]

  9. Comment Preview

Leave a comment

Comments are posted at the discretion of the site owner. Please try to be respectful, insightful and otherwise useful to society as a whole.

(X)HTML is allowed. You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <blockquote cite=""> <cite> <code> <dfn> <em> <kbd> <q cite=""> <samp> <strike> <strong> <sub> <sup> <var>