Browser sniffing is bad, or so the logic goes.
There are occasions though where it makes perfect sense – for example where you are fixing a known bug in a specific version of a browser. A good example is the bug in IE 6 that resets the selected index of a Select tag that has a label and the for attribute. This bug doesn’t affect IE 7 or 8, or any other browser, but does make for a bad user experience if you are doing the right thing and including labels for your select tags.
Microsoft has published some javascript to fix this and I adapted their code to work with with the Prototype Javascript Library.
This fix will look for all select tags on the page (you could adapt it to only look for those with the “for” attribute but I have a sneaking suspicion that if anything that would in fact be a bit slower) and observe the onfocusin and onfocus events as suggested in the knowledge base article.
// Select with 'for' attribute fix for IE
Event.observe(window,'load',function() {
if ( !Prototype.Browser.IE || !(parseInt(navigator.userAgent.substring(navigator.userAgent.indexOf("MSIE")+5))==6) ) return;
$$('select').each(function(eSelect) {
eSelect.observe('focusin',function(e) {
try {
var eSrc = window.event.srcElement;
if ( eSrc ) eSrc.tmpIndex = eSrc.selectedIndex;
} catch(e) {}
});
eSelect.observe('focus',function(e) {
try {
var eSrc = window.event.srcElement;
if ( eSrc ) eSrc.selectedIndex = eSrc.tmpIndex;
} catch(e) {}
})
});
});
Nice fix Pete, found this fix page on google a few days back, come to implement it today so fired up the page again and just noticed the domain name looked familiar. I’m at a Jquery site at the moment so won’t be using this code verbatim but the concept and the link to MSDN sure has help me :-)
Hope your well
Stu
Stu Barker
4 Jun 09 at 6:10 pm