javascript - Canvas not supported in IE fires an exception -
i have canvas element:
<canvas id="canvas" width="300" height="300"> canvas not supported </canvas> code in .js file:
var ct= document.getelementbyid('canvas'); var ctx = document.getelementbyid('canvas').getcontext('2d'); but when load in old versions of ie fires exception method getcontext not supported.
what want write this:
if (!ctx.getcontext) ctx = { getcontext: function () { } }; to rid of exception in ie. should do?
your condition badly structured, , it's hard see plan - going check getcontext() returns every time call it? why not check whether or not exists in first place:
var ct= document.getelementbyid('canvas'); if ('getcontext' in ct) { var ctx = ct.getcontext('2d'); //rest of code } alternatively try 1 of answers this question, or use modernizr feature detection needs.
Comments
Post a Comment