Home >Web Front-end >H5 Tutorial >Detailed explanation of browser compatibility issues with H5 new tags

Detailed explanation of browser compatibility issues with H5 new tags

Y2J
Y2JOriginal
2017-05-22 13:48:543837browse

After all, the HTML5 specification is a specification that has just been defined. There are still some browsers that cannot support the new tags and new attributes, especially browsers of IE8 and below. The following is an introduction to the browser compatibility issue for handling HTML5 new tags. Friends who need it can refer to

After all, the HTML5 specification is a specification that has just been defined. There are also some browsers that cannot support the new tags and New properties, especially for browsers IE8 and below. The following introduces some practical methods for using HTML5 new tags in pages. The purpose is to allow the new tags in HTML5 to receive limited support in low-level browsers and not affect the entire page function.

  • Let the browser recognize the new tags in the HTML5 specification

In IE8 browser Support for HTML5 new tags has not yet been added, so the content in HTML5 new tags cannot be directly displayed in IE8. Fortunately, IE8/IE7/IE6 supports tags generated by the document.createElement method. You can use this feature to make these browsers support HTML5 new tags. The code is as follows:

var e = "abbr, article, aside, audio, canvas, datalist, details, dialog, eventsource, figure, footer, header, hgroup, mark, menu, meter, nav, output, progress, section, time, video".split(', ');
var i= e.length;
while (i--){
    document.createElement(e[i])
}

After the browser supports the new tags, you can also You need to add the default style of the tag:

article,aside,figcaption,figure,footer,header,hgroup,nav,section{display:block}
mark{background:#FF0;color:#000}

In this way, two simple pieces of JavaScript code and CSS code can make browsers of IE8 and below support the new tags in HTML5. Of course, the best way is to directly use the mature framework. There are currently multiple frameworks based on this idea. The most used one is the html5shim framework. The method of using html5shim is very simple. Add the frame in the head part of the page. The can be quoted as :

<!--[if lt IE 9]>
<script> src="http://html5shim.googlecode.com/svn/trunk/html5.js"</script>
<![endif]-->
  • Backward compatibility of new features in HTML5

HTML5 in a broad sense includes HTML5, CSS3 and the new API. Because new features will have more or less browser compatibility issues, it is very necessary to check whether the browser supports this feature when using new features. When the browser does not support new features, appropriate backward compatibility processing can be done. Currently, there is no unified method to detect support for new features. Some new features have corresponding APIs that can be identified, and some new features can only be identified through some techniques. Fortunately, enthusiastic engineers abroad have developed multiple frameworks for detecting new features. Among them, Modernizr has higher detection accuracy and usage rate.

The principle of the Modernizr framework is to automatically detect whether the browser supports new features and add the corresponding class to the 100db36a723c770d327fc0aef2ce13b1 tag. If the browser supports a feature, a class named with the feature name will be added. Otherwise, a class named with the "no-" prefix plus the feature name will be added. At the same time, an object named modernizr will also be generated. By judging the attribute values ​​representing each feature on this object, you can know whether the current browser supports this new feature. The Modernizr framework also includes the functions of the html5shim framework, which allows browsers IE8 and below to support new tags.

The method of using Modernizr is very simple. First, introduce the JavaScript file of the framework in the head part:

<script src="js/modernizr.min.js"></script>

Secondly, add a class named no-js on the html tag :

<html class="no-js">

If the browser does not disable JavaScript, the classes on the html tag will be dynamically replaced and added after the browser loads the page. After loading, the html tag looks like the following:

<html class="js canvas canvastext geolocation rgba hsla no-multiplebgs borderimage borderradius boxshadow opacity no-cssanimations csscolumns no-cssgradients no-cssreflections csstransforms no-csstransforms3d no-csstransitions  video audio cufon-active fontface cufon-ready">

In the CSS code, you can add backward compatibility code by using these classes. The following is an example of using multiple background images:

#nice {
    background: url(background-one.png) top left repeat-x;
}
.multiplebgs #nice {
    background: url(background-one.png) top left repeat-x,url(background-two.png) bottom left repeat-x;
}

This Readers who are interested in the framework can browse the official website of Modernizr for more detailed examples and usage methods.

  • Audio and video compatibility

Audio and video are on the page The Multimedia tag is commonly used, but browser compatibility is quite confusing, so it is treated as a separate topic here. Audio and video are relatively early features that are natively supported by browsers, so audio and video playback is no longer limited to third-party plug-ins, especially on mobile platforms. Audio and video are a big piece of cake, and each browser manufacturer wants to get the biggest piece. This has also led to the differentiation of audio and video formats supported by browsers. The list of audio formats supported by the browser is as follows:

Browser

Version

Supported formats

Internet Explorer

9.0+

MP3, AAC

Chrome

6.0+

##Ogg Vorbis, MP3, WAV (9.0+)

Firefox

3.6+

Ogg Vorbis, WAV

Safari

5.0+

MP3, AAC, WAV

Opera

10.0+

Ogg Vorbis, WAV

大约有80%的浏览器支持HTML5的b97864c2e0ef2353a16c4d64c7734e92标签,但是并没有一种统一的音频格式。从支持的格式来看,要让所有的浏览器可以播放audio元素上的音频,最佳的方式是提供MP3和Ogg两种格式,兼容代码如下:

<audio controls>
    <source src="elvis.mp3" type=&#39;audio/mpeg; codecs="mp3"&#39;>
    <source src="elvis.oga" type=&#39;audio/ogg; codecs="vorbis"&#39;>
    <!-- 向后兼容代码:如,显示提示信息、提供下载链接使用flash播放器等 -->
    浏览器不支持<code>audio</code>标签
</audio>

视频也有和音频类似的状况,如下是浏览器支持视频的格式列表:

浏览器

版本

支持格式

Internet Explorer

9.0+

MP4

Chrome

6.0+

MP4,WebM,Ogg

Firefox

#3.6+

WebM,Ogg

#Safari

#5.0+

#MP4

#Opera

#10.0+

WebM,Ogg

#

从浏览器支持的视频格式来看,最佳的方式是提供WebM和MP4两种格式的视频。兼容代码如下:

<video controls>    
    <source src=video.webm type=video/webm>    
    <source src=video.mp4 type=video/mp4>      
    <!—向后兼容代码: -->      
    <iframe width="480" height="360" src="http://www.youtube.com/embed/xzMUyqmaqcw?rel=0" frameborder="0" allowfullscreen></iframe>  
</video>

【相关推荐】

1. Html5免费视频教程

2. 通过phonegap操作数据库的的教程详解

3. H5中indexedDB 数据库的使用方法详解

4. 为什么现在HTML5的优势越来越大

5. \9和\0可能hack IE11\IE9\IE8无效原因详解

The above is the detailed content of Detailed explanation of browser compatibility issues with H5 new tags. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn