网页怎样检测浏览器是否支持html5?

html-css011

网页怎样检测浏览器是否支持html5?,第1张

有以下两种方法可以判断浏览器是否支持html5:

方法一:

<script>   

function checkhHtml5() {    

if (typeof(Worker) !== "undefined") {   alert("支持HTML5")   }   else {   alert("不支持HTML5")   }  }  

</script>

方法二:

<canvas id="Canvas"></canvas> 

<script>

if (!document.getElementById("Canvas").getContext){

     alert("不支持html5")   

}else{

  alert("支持html5")  

</script>

利用HTML5新标签对象的方法来进行检测,比如Canvas对象的getContext()、Video对象的canPlayType等。如果浏览器支持HTML5,则返回相应的期望值(返回函数体,布尔值为true),否则无法获得期望值(返回undefined,布尔值为false)。

Canvas对象的getContext

// 方法一

/**

 * [supportHtml5 言成科技&HTML5学堂]

 * @return {[type]} [description]

 */

function supportCanvas() {

    return (typeof document.createElement('canvas').getContext === "function")

}

console.log(supportCanvas())

Video对象的canPlayType

// 方法二

/*

 * [supportsVideo 言成科技&HTML5学堂]

 * @return {[type]} [description]

 */

function supportVideo() {

  return !!document.createElement('video').canPlayType

}

console.log(supportVideo())

需要准备的材料分别有:电脑、浏览器、html编辑器。

1、首先,打开html编辑器,新建html文件,例如:index.html。

2、在index.html中的<body>标签中,输入js代码:。

try{

document.createElement("canvas").getContext("2d")

$('body').append('支持canvas')

} catch (e) {

$('body').append('不支持canvas')

}

3、浏览器运行index.html页面,此时会打印出浏览器对html5 canvas的支持程度。