zoukankan      html  css  js  c++  java
  • HTML5特性检測

    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”表示浏览器可能播放此格式

    “”(空字符串)表示浏览器无法播放此格式

    二.检測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;
    }

     

     

     
  • 相关阅读:
    javaweb请求编码 url编码 响应编码 乱码问题 post编码 get请求编码 中文乱码问题 GET POST参数乱码问题 url乱码问题 get post请求乱码 字符编码
    windows查看端口占用 windows端口占用 查找端口占用程序 强制结束端口占用 查看某个端口被占用的解决方法 如何查看Windows下端口占用情况
    javaWeb项目中的路径格式 请求url地址 客户端路径 服务端路径 url-pattern 路径 获取资源路径 地址 url
    ServletRequest HttpServletRequest 请求方法 获取请求参数 请求转发 请求包含 请求转发与重定向区别 获取请求头字段
    HttpServletResponse ServletResponse 返回响应 设置响应头设置响应正文体 重定向 常用方法 如何重定向 响应编码 响应乱码
    Servlet主要相关类核心类 容器调用的过程浅析 servlet解读 怎么调用 Servlet是什么 工作机制
    linq查询语句转mongodb
    winddows rabbitmq安装与配置
    Redis For Windows安装及密码
    出现,视图必须派生自 WebViewPage 或 WebViewPage错误解决方法
  • 原文地址:https://www.cnblogs.com/mfrbuaa/p/3960312.html
Copyright © 2011-2022 走看看