SWFObject 2.2 currently uses an IE detection technique proposed by Andrea Giammarchi in 2009:
It’s a hack that relied on Internet Explorer’s unique handling of vertical spaces (v). Now that it has stopped working, I decided to take a quick look at what others are doing.
One of my favorite approaches is Dean Edwards’ “sniff” technique, which takes advantage of Microsoft’s conditional compilation.
var isIE = /*@cc_on!@*/!1;Code language: PHP (php)
One way to get around the compressor issue (as pointed out by a commenter on Dean Edwards’ post) is to wrap the conditional compilation statement in an
eval() is evil, I won’t use this approach.
One of the most long-standing methods of detecting Internet Explorer is to examine the userAgent string:
However, this isn’t foolproof, as most browsers allow you to change the userAgent string at will. For example, I changed Safari’s userAgent string to report itself as Internet Explorer 8, and
So far, the only test I’ve found that seems to fit the bill is the old
Is it perfect? Not a chance, but it has a lot of upside: it works in IE9, doesn’t get damaged when compressed, doesn’t rely on any hacky tricks (as fun as they might be), and isn’t affected by spoofed userAgent strings.
What are the Frameworks using?
jQuery uses userAgent sniffing, and includes a warning that it’s unreliable. MooTools uses a combination of properties from the
navigator object, including
navigator.platform. It’s interesting to note that MooTools previously used feature detection to infer the browser brand, but changes introduced by Firefox 3.6 prompted the MooTools team to switch to a userAgent-based detection method instead. (Nicholas Zakas wrote an interesting blog post about MooTools’ prior detection technique.)
Dustin Diaz’s Bowser detection script uses userAgent exclusively.
Why sniff in the first place?
Before you get down on me for even talking about sniffing, relax, I agree with you. I much prefer feature detection to browser sniffing; this is especially important with our quickly changing browser landscape: HTML5, web storage, geolocation, etc. (Modernizr is a great tool for modern feature detection.)
So we carry on. I use feature detection for just about everything, but once in a while I need a global “are you Internet Explorer?”, and browser sniffing fits the bill.
Gidday Pipwerks – I felt obliged to try and add in some browser sniffing after reading, I found the following worked a treat – it isn’t “elegant” but for IE it’s pretty solid:
Am I IE?
I did it with YUI3 initially, in the same # of lines of code, but reduced it to good ole JS.
I’d also tried to avoid string testing, by dynamically creating script tags with a variable test inside – but then remembered that IE borks on that approach – maybe there’s a way to do it.
but either way, creating an element with an inline conditional comment works as well as the comments themselves!
Doh – the important bit got stripped out when I submitted – but basically the innerHTML is set to be an IE conditional comment, all I put inside the comment was the string: “IE9”, which I then test for.
@danjah i fixed the post for you. thanks for the suggestion, it’s an interesting approach.
Comments are closed.