DIV CSS 佈局教程網

您现在的位置: DIV+CSS佈局教程網 >> 網頁腳本 >  >> HTML基礎知識 >> HTML5詳解
HTML5 特性檢測程式碼速查大全
編輯:HTML5詳解     

這篇把常用的 HTML5 特性檢測寫法整理成一份可直接複製的速查表,依主題分類。每一段程式都會回傳布林值,方便直接放進 if 條件裡判斷。

音訊 Audio

基本支援:

return !!document.createElement('audio').canPlayType;

MP3 格式:

var a = document.createElement('audio');
return !!(a.canPlayType && a.canPlayType('audio/mpeg;').replace(/no/, ''));

Vorbis 格式:

var a = document.createElement('audio');
return !!(a.canPlayType && a.canPlayType('audio/ogg; codecs="vorbis"').replace(/no/, ''));

WAV 格式:

var a = document.createElement('audio');
return !!(a.canPlayType && a.canPlayType('audio/wav; codecs="1"').replace(/no/, ''));

AAC 格式:

var a = document.createElement('audio');
return !!(a.canPlayType && a.canPlayType('audio/mp4; codecs="mp4a.40.2"').replace(/no/, ''));

Canvas

基本支援:

return !!document.createElement('canvas').getContext;

文字 API:

var c = document.createElement('canvas');
return c.getContext && typeof c.getContext('2d').fillText == 'function';

影片 Video

基本支援:

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

WebM 格式:

var v = document.createElement('video');
return !!(v.canPlayType && v.canPlayType('video/webm; codecs="vp8, vorbis"').replace(/no/, ''));

H.264 格式:

var v = document.createElement('video');
return !!(v.canPlayType && v.canPlayType('video/mp4; codecs="avc1.42E01E, mp4a.40.2"').replace(/no/, ''));

Theora 格式:

var v = document.createElement('video');
return !!(v.canPlayType && v.canPlayType('video/ogg; codecs="theora, vorbis"').replace(/no/, ''));

字幕軌(captions):

return 'track' in document.createElement('track');

封面圖(poster):

return 'poster' in document.createElement('video');

表單與互動新元素

command      return 'type' in document.createElement('command');
datalist     return 'options' in document.createElement('datalist');
details      return 'open' in document.createElement('details');
device       return 'type' in document.createElement('device');
meter        return 'value' in document.createElement('meter');
output       return 'value' in document.createElement('output');
progress     return 'value' in document.createElement('progress');
time         return 'valueAsDate' in document.createElement('time');
contentEditable   return 'isContentEditable' in document.createElement('span');

表單驗證(constraint validation):

return 'noValidate' in document.createElement('form');

iframe 沙箱與 srcdoc:

return 'sandbox' in document.createElement('iframe');
return 'srcdoc' in document.createElement('iframe');

input 的 autofocus 與 placeholder:

return 'autofocus' in document.createElement('input');
return 'placeholder' in document.createElement('input');

input 型別檢測

這一類全都用同一個套路:建立 input、改型別、看有沒有被退回 text。支援的型別包括 color、email、number、range、search、tel、url、date、time、datetime、datetime-local、month、week。寫法如下:

var i = document.createElement('input');
i.setAttribute('type', 'color');
return i.type !== 'text';

把 'color' 換成上表任一型別即可。用 Modernizr 則可寫成 Modernizr.inputtypes.date 這類形式。

進階 API

Cross-document messaging   return !!window.postMessage;
Drag-and-drop              return 'draggable' in document.createElement('span');
File API                   return typeof FileReader != 'undefined';
Geolocation                return !!navigator.geolocation;
History                    return !!(window.history && window.history.pushState && window.history.popState);
Local storage              return ('localStorage' in window) && window['localStorage'] !== null;
Session storage            try { return ('sessionStorage' in window) && window['sessionStorage'] !== null; } catch(e) { return false; }
Microdata                  return !!document.getItems;
Offline web applications   return !!window.applicationCache;
Server-sent events         return typeof EventSource !== 'undefined';
Web SimpleDB               return !!window.indexedDB;
Web Sockets                return !!window.WebSocket;
Web SQL Database           return !!window.openDatabase;
Web Workers                return !!window.Worker;
Undo                       return typeof UndoManager !== 'undefined';

SVG

return !!(document.createElementNS && document.createElementNS('https://www.w3.org/2000/svg', 'svg').createSVGRect);

HTML 中內嵌 SVG:

var e = document.createElement('div');
e.innerHtml = '<svg></svg>';
return !!(window.SVGSVGElement && e.firstChild instanceof window.SVGSVGElement);

幾個使用提醒

第一,sessionStorage 的檢測特別包了 try/catch,因為某些瀏覽器在停用 Cookie 或處於隱私模式時,存取 storage 會直接丟出例外,不處理就會讓整段腳本中斷。localStorage 在實務上也建議比照辦理。

第二,.replace(/no/, '') 這個小技巧是因為 canPlayType 可能回傳 "no" 這類字串,把它清掉之後剩下的內容才不會讓判斷誤判為真。

第三,這份清單反映的是 HTML5 早期的技術版圖,其中 Web SQL Database 等項目已被淘汰,現行的標準走向了 IndexedDB。真正開發時,與其逐條手寫,不如直接引入 Modernizr 這類成熟函式庫,並搭配 @supports 等 CSS 層級的特性檢測一起使用。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved