search
HomeWeb Front-endH5 TutorialMethods to detect browser support for HTML5 and CSS3_html5 tutorial skills

HTML5, CSS3 and other related technologies such as Canvas, WebSocket, etc. have brought web application development to a new level. This technology combines HTML, CSS, and JavaScript to develop the effects of a desktop application. Although HTML5 promises a lot, in reality the browsers that support HTML5 and the HTML5 standard itself are not yet mature enough. It is unrealistic not to worry about browser support at all now, and it will take time. Therefore, when we decide to use HTML5 technology to develop web applications, we need to detect the features supported by the browser.

Modernizr can help you check the HTML5 features supported by the browser.

The following code detects whether the browser supports Canvas:


Copy the code
The code is as follows:
<script><br /> window.onload = function () {<br /> if (canvasSupported()) {<br /> alert('canvas supported');<br /> }<br /> };<br /> <br /> function canvasSupported() {<br /> var canvas = document.createElement('canvas');<br /> return (canvas.getContext && canvas.getContext('2d'));<br /> }<br /> </script>

The following code detects whether the browser supports local storage:

Copy the code
The code is as follows:

<script><br /> window.onload = function () {<br /> if (localStorageSupported()) {<br /> alert('local storage supported');<br /> }<br /> }; <br /> <br /> function localStorageSupported() {<br /> try {<br /> return ('localStorage' in window && window['localStorage'] != null);<br /> }<br /> catch(e) { }<br /> return false;<br /> }<br /></script>

In the above two examples, we can intuitively check the browser features to ensure that the functions we apply on the corresponding browsers can operate normally.


The advantage of using Modernizr is that you don’t need to check each item like this. There is a simpler way. Let’s start below:

When I first heard about the Moderizr project, I thought it was a JS library that allows some old browsers to support HTML5. In fact, it is not. It is mainly a detection function.

Modernizr can be accessed through the URL http://modernizr.com. The website also provides a custom script function. You can determine what features you need to detect and generate the corresponding JS files accordingly, which can reduce Unnecessary JS code.
2015625153003697.png (690×533)

Once you download the Modernizr JS file, you can introduce it into the web page through the <script> tag. <br /> <br /><br><div class="msgheader"><div class="right"><span style="CURSOR: pointer" onclick="copycode(getid('phpcode11'));"><u>Copy codeThe code is as follows:<div class="msgborder" id="phpcode11"><script src="Scripts/Modernizr .js" type="text/javascript"></script>

Detect HTML elements

Once we introduce Modernizr on the page, we can use it immediately. We can declare different CSS classes in the element. These classes define the features that need to be supported or not supported, and the features that are not supported. The class name is generally no-FeatureName, such as no-flexbox. Here's an example that works on chrome:


Copy code
The code is as follows:

You can also use this to determine whether the browser has JavaScript support enabled:


Copy the code
The code is as follows :

You can see some introductory examples in HTML5 Boilerplate (http://html5boilerplate.com) or Initializr (http://initializr.com). According to the above steps, adding the no-js class can determine the browser Whether JavaScript support is enabled.

Use HTML5 and CSS3 features

The CSS attributes you add to the tag can directly define the required styles in CSS, for example:

Copy code
The code is as follows:

.boxshadow #MyContainer {
border: none;
-webkit-box-shadow: #666 1px 1px 1px;
-moz-box-shadow: #666 1px 1px 1px;
}

.no-boxshadow #MyContainer {
border: 2px solid black;
}

If the browser supports box-shadows, the boxshadow CSS class will be added to the element, otherwise the no-boxshadow class will be used. Assuming that the browser does not support box-shadow, we can use other styles to define it.


In addition, we can also use Modernizr objects to operate this behavior. For example, the following code is used to detect whether the browser supports Canvas and local storage:


Copy code
The code is as follows:

$(document).ready(function ( ) {

if (Modernizr.canvas) {
//Add canvas code
}

if (Modernizr.localstorage) {
//Add local storage code
}

});

The global Modernizr object can also be used to test whether CSS3 features are supported:

Copy the code
The code is as follows:

$(document).ready(function () {

if (Modernizr.borderradius) {
$('#MyDiv').addClass('borderRadiusStyle');
}

if (Modernizr.csstransforms) {
$('#MyDiv').addClass('transformsStyle');
}

});


Use Modernizr to load the script

In the event that the browser does not support certain features, you can not only provide a good backup solution, but also load shim/polyfill scripts to fill in the missing features where appropriate (want to learn more about shims /polyfills, please see https://github.com/Modernizr/Modernizr/wiki/HTML5-Cross-Browser-Polyfills). Modernizr has a built-in script loader that can be used to test a feature and detect when the feature is invalid. when loading another script. The script loader is built into Modernizr and is effectively a standalone yepnope (http://yepnopejs.com) script. The script loader is very easy to use and it is based on the availability of specific browser features. It would really simplify the process of loading scripts.

You can use Modernizr's load() method to dynamically load scripts. This method accepts attributes that define the function under test (test attribute), such as the script to be loaded after the test is successful (yep attribute), and the script to be loaded after the test fails. script (nope attribute), and a script that should be loaded regardless of whether the test succeeds or fails (both attribute). Examples of using load() and its attributes are as follows:

Copy the code
The code is as follows:

Modernizr.load({
test: Modernizr.canvas,
yep: 'html5CanvasAvailable.js',
nope: 'excanvas.js',
both: 'myCustomScript.js'
});


In this example, Modernizr will also test whether the canvas function is supported when loading the script. If the target browser supports HTML5 canvas, it will load the html5CanvasAvailable.js script and the myCustomScript.js script (in this example, use the yep attribute A bit far-fetched - this is just to demonstrate how properties in the load() method are used). Otherwise, the excanvas.js polyfill script would be loaded to add support for browsers prior to IE9. Once excanvas.js is loaded, myCustomScript .js will also be loaded next.

Since Modernizr will handle loading scripts, you can use it to do other things. For example, you can use Modernizr to load local scripts when the third-party CDN provided by Google or Microsoft does not work. Modernizr documentation An example of providing a local jQuery fallback process after the CDN fails is provided in:
The code will first try to load jQuery from Google CND. Once the script download is completed (or the download fails), a method will be called. This method will check jQuery Whether the object is valid, if not, load the local jQuery script. And then load a script named needs-jQuery.js.

The last thing I want to say is that if you plan to develop web applications based on HTML5 and CSS3, then Modernizr is an indispensable tool for you, unless, unless you confirm that the browsers used by all your customers support what you write code.

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
H5: Tools, Frameworks, and Best PracticesH5: Tools, Frameworks, and Best PracticesApr 11, 2025 am 12:11 AM

The tools and frameworks that need to be mastered in H5 development include Vue.js, React and Webpack. 1.Vue.js is suitable for building user interfaces and supports component development. 2.React optimizes page rendering through virtual DOM, suitable for complex applications. 3.Webpack is used for module packaging and optimize resource loading.

The Legacy of HTML5: Understanding H5 in the PresentThe Legacy of HTML5: Understanding H5 in the PresentApr 10, 2025 am 09:28 AM

HTML5hassignificantlytransformedwebdevelopmentbyintroducingsemanticelements,enhancingmultimediasupport,andimprovingperformance.1)ItmadewebsitesmoreaccessibleandSEO-friendlywithsemanticelementslike,,and.2)HTML5introducednativeandtags,eliminatingthenee

H5 Code: Accessibility and Semantic HTMLH5 Code: Accessibility and Semantic HTMLApr 09, 2025 am 12:05 AM

H5 improves web page accessibility and SEO effects through semantic elements and ARIA attributes. 1. Use, etc. to organize the content structure and improve SEO. 2. ARIA attributes such as aria-label enhance accessibility, and assistive technology users can use web pages smoothly.

Is h5 same as HTML5?Is h5 same as HTML5?Apr 08, 2025 am 12:16 AM

"h5" and "HTML5" are the same in most cases, but they may have different meanings in certain specific scenarios. 1. "HTML5" is a W3C-defined standard that contains new tags and APIs. 2. "h5" is usually the abbreviation of HTML5, but in mobile development, it may refer to a framework based on HTML5. Understanding these differences helps to use these terms accurately in your project.

What is the function of H5?What is the function of H5?Apr 07, 2025 am 12:10 AM

H5, or HTML5, is the fifth version of HTML. It provides developers with a stronger tool set, making it easier to create complex web applications. The core functions of H5 include: 1) elements that allow drawing graphics and animations on web pages; 2) semantic tags such as, etc. to make the web page structure clear and conducive to SEO optimization; 3) new APIs such as GeolocationAPI support location-based services; 4) Cross-browser compatibility needs to be ensured through compatibility testing and Polyfill library.

How to do h5 linkHow to do h5 linkApr 06, 2025 pm 12:39 PM

How to create an H5 link? Determine the link target: Get the URL of the H5 page or application. Create HTML anchors: Use the <a> tag to create an anchor and specify the link target URL. Set link properties (optional): Set target, title, and onclick properties as needed. Add to webpage: Add HTML anchor code to the webpage where you want the link to appear.

How to solve the h5 compatibility problemHow to solve the h5 compatibility problemApr 06, 2025 pm 12:36 PM

Solutions to H5 compatibility issues include: using responsive design that allows web pages to adjust layouts according to screen size. Use cross-browser testing tools to test compatibility before release. Use Polyfill to provide support for new APIs for older browsers. Follow web standards and use effective code and best practices. Use CSS preprocessors to simplify CSS code and improve readability. Optimize images, reduce web page size and speed up loading. Enable HTTPS to ensure the security of the website.

How to generate links with h5How to generate links with h5Apr 06, 2025 pm 12:33 PM

h5 pages can generate links in two ways: create links manually or use short link services. By manually creating, you just need to copy the URL of the h5 page; through the short link service, you need to paste the URL into the service and then get the shortened URL.

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)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.