IE7 Non-Standard Event Handlers

Recently I had a problem where, when using IE7 (a rare occurrence and only ever for testing purposes) an XHTML ‘SELECT’ element would not open properly. On first click it merely took focus, and it would only open with another click. On Firefox, needless to say, everything was perfectly fine.

Further investigation with a colleague revealed that the incorrect IE7 behaviour only occurred when the element had an ‘onfocus’ event handler.

30 seconds of Googling revealed SELECTs in IE7 – use onfocusin! which spoke of an IE-specific event handler, ‘onfocusin’, which would solve the bug.

I left the ‘onfocus’ event handler there, added an identical ‘onfocusin’ handler (all the handler did was change background colour so being called twice wouldn’t matter) and all was well. Except, of course, the page would not validate, because ‘onfocusin’ is not a W3C standard handler – it’s just something dreamt up by Micro$oft.

So, if you need to rectify this behaviour in your pages, and you want pages that validate, you’re going to have to do some server-side browser detection and only include the ‘onfocusin’ handler if the user’s browser is IE7 (earlier IEs are fine), otherwise include the normal and standards-compliant ‘onfocus’.

And if you’re thinking this looks like a return to the bad old days of designers having to use branching code to make sites work in IE5 and Netscape Communicator, you’d be right. Micro$oft are still making life as difficult as possible for designers who don’t want to restrict their users to a single browser. And this famously arrogant approach still seems to be deliberate – there’s no reason I can think of for introducing two new event handlers (yes, they created ‘onfocusout’ as well – sigh).