When feature detection fails - thank you MS... not.
Sunday, 28. December 2008, 13:41:06
I thought: hey, I don't use browser sniffing except for detecting MSIE, and then let MSIE do the detection itself using conditional comments. Instead, I use feature detection. My scripts should be fine.
Right?
Wrong.
In the Canvas application we discussed a few weeks ago, and in every other javascript-heavy application we produce, feature detection determines what a browser can and can't do. Almost anything is checked before using it. Including the ability of the browser to create an HTMLCanvasElement.
MSIE doesn't support the Canvas functionality yet. Not even in version 8 beta 2. The competition, obviously, does, for better and for worse. Some browsers have an awful implementation. Maybe MS thinks it's better to wait with publishing their Canvas support until their development nears perfection.
So, MSIE doesn't support the Canvas element. As a result, what do I expect from the execution of the following in MSIE?
var c = document.createElement('canvas');
I expect an 'undefined', or a false, or a null. Anything except an HTMLCanvasElement.
Guess what MSIE 8b2 does?
No, I said: Guess.
Well, OK, I'll spill the beans. MSIE 8b2 creates a new HTMLCanvasElement. It doesn't have Canvas support, but it will still create the element. So here we are, with an HTMLCanvasElement, but without a context, or any predefined properties, apart from it being an element node.
Our feature detection was based on the ability of the browser to create the HTMLCanvasElement. We assumed that any browser that doesn't support the Canvas functionality, would also refuse to create that element. We would detect that, and hand it an image element instead. We were wrong.
Now we have to switch our detection to something else. Like the ability to get a 2d-context. Oh wait... we did that already, which is why our scripts didn't break anywhere. We just ended up with emptiness where an image should have appeared.
Thank you, Microsoft. Not.








