HTML5特性檢測

來源:互聯網
上載者:User

標籤:style   http   color   os   io   使用   ar   for   資料   

HTML5特性檢測:

   1.檢測全域對象:諸如window或navigator是否擁有特定的屬性

   2.建立元素:檢測該元素的DOM對象是否擁有特定的屬性

   3.建立元素:檢測該元素的DOM對象是否擁有特定的方法

   4.建立元素:賦予該元素的DOM對象設定的屬性值,檢測瀏覽器

              是否保留該屬性值

Modernizr:HTML5特性檢測庫,用於檢測瀏覽器是否支援HTML5

         和CSS3特性.下載Development版http://www.modernizr.com/

eg.   

Ⅰ.檢測瀏覽器是否支援canvas API

1. html5特性檢測2:

<html>

<head>

<title>Dive into HTML5</title></head>

<body>

<script>

function supports_canvas() {

return !!document.createElement(‘canvas‘).getContext;}

alert(supports_canvas());

</script></body></html>

2.使用modernizr庫提供的方法檢測

<html><head><meta charset="utf-8">

<title>Dive into HTML5</title>

<script src="modernizr.custom.57110.js" ></script>

</head><body><script>

if (Modernizr.canvas) {

alert("OK"); }   // let‘s draw some shapes!

 else {

alert("no");}     // no native canvas support available

</script></body></html>

Ⅱ.檢測瀏覽器是否支援HTML5 viedo

1.使用HTML5特性檢測2

function supports_video(){

       return !!document.CreateElement(“video”).canPlayType;}

2.使用modernizr庫提供的方法檢測

if(modernizr.video){

let‘s play some video!}

else{

// no native video support available

// maybe check for QuickTime or Flash instead }

Ⅲ.檢測瀏覽器是否支援某種視頻格式

一.檢測MPEG-4容器裡的h.264編碼格式的視頻和AACLC格式的音頻(mac和Iphone支援)

1.使用HTML5檢測特性3

     function supports_h264_baseline_video(){

        if(!supports_video()){return false;}

        var v=document.createElement(“video”);

        return v.canPlayType(‘video/mp4;codecs=”avc1.42E01E,mp4a.40.2”’);}

canPlayType()方法返回一個字串:

“probably”表示瀏覽器有充分把握能夠播放此格式

 “maybe”表示瀏覽器可能播放此格式

“”(Null 字元串)表示瀏覽器無法播放此格式

二.檢測Ogg容器內Theora編碼格式的視頻和Vorbis編碼格式的音頻(Mozilla Firefox等開源瀏覽器支援)

1.使用HTML5檢測特性3

  function supports_ogg_theora_video(){

         if(!supports_video()){return false;}

         var v=document.createElement(“video”);

         return v.canPlayType(‘video/ogg;codecs=”theora,vorbis”’);}

三.檢測 Matroska容器內webM視頻編碼格式和Vorbis編碼格式的音頻

1.使用HTML5檢測特性3

function supports_webm_video() {

if (!supports_video()) { return false; }

var v = document.createElement("video");

return v.canPlayType(‘video/webm; codecs="vp8, vorbis"‘);

使用modernizr庫提供的方法檢測瀏覽器是否支援各種HTML5格式

    if (Modernizr.video) {

// let‘s play some video! but what kind?

if (Modernizr.video.ogg) {

// try Ogg Theora + Vorbis in an Ogg container

} else if (Modernizr.video.h264){// try H.264 video + AAC audio in an MP4 container

}}

Ⅳ.檢測瀏覽器是否支援本機存放區。
1.檢測方法1
function supports_local_storage() {
return (‘localStorage‘ in window) && window[‘localStorage‘] !== null;
}

2.使用modernizr庫
     if (Modernizr.localstorage) {
               // window.localStorage is available!}
       else {
           // no native support for local storage :(
          // maybe try Gears or another third-party solution
}

Ⅴ.檢測瀏覽器是否支援Web Workers。
1.檢測方法1
   function supports_web_workers() {
return !!window.Worker;
}
2.使用modernizr庫
    if (Modernizr.webworkers) {
// window.Worker is available!
} else {
// no native support for web workers :(
// maybe try Gears or another third-party solution
}
Ⅵ.檢測瀏覽器是否支援離線web應用(Offline Web Applications)
 1.檢測方法1
    function supports_offline() {
return !!window.applicationCache;
}
2.使用modernizr庫
  if (Modernizr.applicationcache) {
// window.applicationCache is available!
} else {
// no native support for offline :(
// maybe try Gears or another third-party solution
}
Ⅶ.檢測瀏覽器是否支援地理位置應用
 1.檢測方法1
    function supports_geolocation() {
return !!navigator.geolocation;
}
2.使用modernizr庫
  if (Modernizr.geolocation) {
// let‘s find out where you are!
} else {
// no native geolocation support available :(
// maybe try Gears or another third-party solution
}
Ⅷ。檢測瀏覽器是否支援輸入框類型
1.檢測方法4
function supports_inputtypes{
   var i = document.createElement("input");
   i.setAttribute("type", "color");
   return i.type !== "text";
}
2.使用modernizr庫
  if (!Modernizr.inputtypes.date) {
// no native support for <input type="date"> :(
// maybe build one yourself with
// Dojo
// or jQueryUI
}
Ⅸ.檢測瀏覽器是否支援站位文本(Placeholder Text)
   1.檢測方法2
      function supports_input_placeholder() {
var i = document.createElement(‘input‘);
return ‘placeholder‘ in i;
}
2.使用modernizr庫
  if (Modernizr.input.placeholder) {
// your placeholder text should already be visible!
} else {
// no placeholder support :(
// fall back to a scripted solution
}
Ⅹ。檢測瀏覽器是否支援自己主動聚焦
    1.檢測方法2
        function supports_input_autofocus() {
var i = document.createElement(‘input‘);
return ‘autofocus‘ in i;
}
2.使用modernizr庫
if (Modernizr.input.autofocus) {
// autofocus works!
} else {
// no autofocus support :(
// fall back to a scripted solution
}
 Ⅺ。檢測瀏覽器是否支援HTML5微資料API
 1.檢測方法1
        function supports_microdata_api() {
return !!document.getItems;
}

 

 

 

HTML5特性檢測

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.