HTML5特性检测-检测技术
4种基本技术可以用于检测浏览器是否支持某种html5特性,从简单到复杂的顺序:
1、检测全局对象(window或navigator)是否拥有特定的属性。
如果浏览器支持地理位置API的话,全局的navigator对象上会有一个名为geolocation的属性,反之,navigator对象上该属性的值为undefined:
function supports_geolocation(){ return !!navigator.geolocation; }
如果不想写这个方法的话,可以使用modernizr库提供的方法检测浏览器是否支持地理位置API:
if(Modernizr.geolocation){ do something} else{ do something }
2、创建一个元素,然后检测该元素的DOM对象是否拥有特定的属性。以检测画布特性为例。
function supports_canvas(){ return !!document.createElement_x_x('canvas').getContext; }
return !!document.createElement_x_x('canvas').getContext;这一句是创建一个虚拟的
如果不想写这个方法的话,可以使用modernizr库提供的方法检测浏览器是否支持canvas API:
if(Modernizr.canvas){ do something} else{ do something }
3、创建一个元素,然后检测该元素的DOM对象是否拥有特定的方法,同时调用这个方法并检查它的返回值。以检测支持视频格式为例。
function supports_video(){ return !!document.createElement_x_x('video').canPlayType; }
如果浏览器支持HTML5 video,被创建
如果不想写这个方法的话,可以使用modernizr库提供的方法检测浏览器是否支持video API:
if(Modernizr.video){ do something} else{ do something }
4、创建一个元素,给这个元素的DOM对象设定特定的属性值,然后检查浏览器是否保留了该属性值。以检测支持的类型为例。
首先创建一个虚拟的元素:var i=document.createElement_x_x("input");
元素默认为文本类型,接下来将元素的类型设置成要检测的类型: i.setAttribute("type","color");
如果浏览器支持次特定的输入框类型,那么设置的type值会被保留,反之,依然为文本类型。
return i.type!=="text"; if(!Modernizr.inputtypes.date){ //浏览器有没有提供,type="date">原生支持 }
以上就是HTML5特性检测-检测技术的内容,更多相关内容请关注PHP中文网(www.php.cn)!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Guide to Table Border in HTML. Here we discuss multiple ways for defining table-border with examples of the Table Border in HTML.

This is a guide to Nested Table in HTML. Here we discuss how to create a table within the table along with the respective examples.

Guide to HTML margin-left. Here we discuss a brief overview on HTML margin-left and its Examples along with its Code Implementation.

Guide to HTML Table Layout. Here we discuss the Values of HTML Table Layout along with the examples and outputs n detail.

Guide to Moving Text in HTML. Here we discuss an introduction, how marquee tag work with syntax and examples to implement.

Guide to the HTML Ordered List. Here we also discuss introduction of HTML Ordered list and types along with their example respectively

Guide to HTML onclick Button. Here we discuss their introduction, working, examples and onclick Event in various events respectively.

Guide to HTML Input Placeholder. Here we discuss the Examples of HTML Input Placeholder along with the codes and outputs.
