search
HomeWeb Front-endH5 TutorialResolve browser compatibility issues with HTML5 new tags

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])
}

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}

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]-->

  • 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>

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

<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 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;
}

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>

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

浏览器

版本

支持格式

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>

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!

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
How to Add Audio to My HTML5 Website?How to Add Audio to My HTML5 Website?Mar 10, 2025 pm 03:01 PM

This article explains how to embed audio in HTML5 using the <audio> element, including best practices for format selection (MP3, Ogg Vorbis), file optimization, and JavaScript control for playback. It emphasizes using multiple audio f

How to Use HTML5 Forms for User Input?How to Use HTML5 Forms for User Input?Mar 10, 2025 pm 02:59 PM

This article explains how to create and validate HTML5 forms. It details the <form> element, input types (text, email, number, etc.), and attributes (required, pattern, min, max). The advantages of HTML5 forms over older methods, incl

How do I use the HTML5 Page Visibility API to detect when a page is visible?How do I use the HTML5 Page Visibility API to detect when a page is visible?Mar 13, 2025 pm 07:51 PM

The article discusses using the HTML5 Page Visibility API to detect page visibility, improve user experience, and optimize resource usage. Key aspects include pausing media, reducing CPU load, and managing analytics based on visibility changes.

How do I use viewport meta tags to control page scaling on mobile devices?How do I use viewport meta tags to control page scaling on mobile devices?Mar 13, 2025 pm 08:00 PM

The article discusses using viewport meta tags to control page scaling on mobile devices, focusing on settings like width and initial-scale for optimal responsiveness and performance.Character count: 159

How do I handle user location privacy and permissions with the Geolocation API?How do I handle user location privacy and permissions with the Geolocation API?Mar 18, 2025 pm 02:16 PM

The article discusses managing user location privacy and permissions using the Geolocation API, emphasizing best practices for requesting permissions, ensuring data security, and complying with privacy laws.

How to Create Interactive Games with HTML5 and JavaScript?How to Create Interactive Games with HTML5 and JavaScript?Mar 10, 2025 pm 06:34 PM

This article details creating interactive HTML5 games using JavaScript. It covers game design, HTML structure, CSS styling, JavaScript logic (including event handling and animation), and audio integration. Essential JavaScript libraries (Phaser, Pi

How do I use the HTML5 Drag and Drop API for interactive user interfaces?How do I use the HTML5 Drag and Drop API for interactive user interfaces?Mar 18, 2025 pm 02:17 PM

The article explains how to use the HTML5 Drag and Drop API to create interactive user interfaces, detailing steps to make elements draggable, handle key events, and enhance user experience with custom feedback. It also discusses common pitfalls to a

How do I use the HTML5 WebSockets API for bidirectional communication between client and server?How do I use the HTML5 WebSockets API for bidirectional communication between client and server?Mar 12, 2025 pm 03:20 PM

This article explains the HTML5 WebSockets API for real-time, bidirectional client-server communication. It details client-side (JavaScript) and server-side (Python/Flask) implementations, addressing challenges like scalability, state management, an

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft