html5 - How to handle canvas unavailability in javascript? -


i've html page uses html 5 canvas. i've used below code creating canvas object.

<canvas id="testcanvas" width="200" height="200">     canvas not supported </canvas> 

this works expected , displays text "canvas not supported" in browsers ie 8. however, there problem below javascript associated canvas.

<script>     var testcanvas = document.getelementbyid("testcanvas");     var testcanvascontext = testcanvas.getcontext("2d"); </script>  

the above code throws error message object doesn't support property or method in browsers don't support canvas. what's correct approach handle in javascript?

please not suggest use libraries excanvas.

try following :

<script>    var testcanvas = document.getelementbyid("testcanvas");    var testcanvascontext = window.canvasrenderingcontext2d && testcanvas.getcontext("2d");    if (testcanvascontext) {       // stuff    } </script> 

Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -