Home > Web Front-end > H5 Tutorial > Resolve browser compatibility issues with HTML5 new tags

Resolve browser compatibility issues with HTML5 new tags

怪我咯
Release: 2017-04-30 11:00:10
Original
5243 people have browsed it

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 through 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])
}
Copy after login

After the browser supports the new tag, you also 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}
Copy after login

In this way, two simple JavaScript codes and CSS codes are enough Let browsers of IE8 and below support the new tags in HTML5. Of course, the best way is to directly use a mature framework. There are currently multiple frameworks based on this idea. The most commonly used framework is the html5shim framework. The method of using html5shim is very simple. Just add a reference to the framework in the head part of the page:

<!--[if lt IE 9]>
<script> src="http://html5shim.googlecode.com/svn/trunk/html5.js"</script>
<![endif]-->
Copy after login

  • Backward compatibility of new features in HTML5

HTML5 in a broad sense includes HTML5, CSS3 and new APIs. 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 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>
Copy after login

Secondly, add a name to the html tag Classes for no-js:

<html class="no-js">
Copy after login

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">
Copy after login

In the CSS code, you can add backward compatibility code by using these classes. The following is a method using multiple background images. Example:

#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;
}
Copy after login

Readers interested in this framework can browse the official website of Modernizr for more detailed examples and usage.

  • Audio and video compatibility

Audio and video are on the page Multimedia tags are commonly used in the media, 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的

<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>
Copy after login

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

浏览器

版本

支持格式

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>
Copy after login

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

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template