How to Check if the browser supports HTML5?

后端 未结 5 900
执笔经年
执笔经年 2020-12-10 01:10

EDIT I have changed some Javascript now, so if I can find a javascript function that detects HTML5 Video support, it should work.

I have a

相关标签:
5条回答
  • 2020-12-10 01:31

    One liner check...

    // Plain JavaScript
    (typeof document.createElement('canvas').getContext === "function") 
    
    // Or... Using lodash
    _.isFunction(document.createElement('canvas').getContext)
    
    0 讨论(0)
  • 2020-12-10 01:35

    Here is how w3schools does it:

    function checkVideo()
    {
    if(!!document.createElement('video').canPlayType)
      {
      var vidTest=document.createElement("video");
      oggTest=vidTest.canPlayType('video/ogg; codecs="theora, vorbis"');
      if (!oggTest)
        {
        h264Test=vidTest.canPlayType('video/mp4; codecs="avc1.42E01E, mp4a.40.2"');
        if (!h264Test)
          {
          document.getElementById("checkVideoResult").innerHTML="Sorry. No video support."
          }
        else
          {
          if (h264Test=="probably")
            {
            document.getElementById("checkVideoResult").innerHTML="Yeah! Full support!";
            }
          else
            {
            document.getElementById("checkVideoResult").innerHTML="Meh. Some support.";
            }
          }
        }
      else
        {
        if (oggTest=="probably")
          {
          document.getElementById("checkVideoResult").innerHTML="Yeah! Full support!";
          }
        else
          {
          document.getElementById("checkVideoResult").innerHTML="Meh. Some support.";
          }
        }
      }
    else
      {
      document.getElementById("checkVideoResult").innerHTML="Sorry. No video support."
      }
    }
    
    0 讨论(0)
  • 2020-12-10 01:40

    The better solution is to use something like Modernizr to do your feature detection on the client-side.Modernizr is an open source, MIT-licensed JavaScript library that detects support for many HTML5 & CSS3 features. If your browser does not support the canvas API, the Modernizr.canvas property will be false.

    if (Modernizr.canvas) {
      // let's draw some shapes!
    } else {
      // no native canvas support available :(
    }
    

    Ref

    Another solution if you are using JQuery: Checking for support for the canvas element of HTML 5

    var test_canvas = document.createElement("canvas") //try and create sample canvas element
    var canvascheck=(test_canvas.getContext)? true : false //check if object supports getContext() method, a method of the canvas element
    alert(canvascheck) //alerts true if browser supports canvas element
    

    Ref

    0 讨论(0)
  • 2020-12-10 01:42

    Check out everything at Dive into HTML5 especially the 'Detecting HTML5 Techniques' section. It has pretty much everything you may need.

    0 讨论(0)
  • 2020-12-10 01:43

    Have you had a look at http://www.modernizr.com/docs/#features-css

    It can do feature detection

    0 讨论(0)
提交回复
热议问题