search
HomeWeb Front-endH5 TutorialDetailed introduction to HTML5 API browser support detection

HTML5 has developed to the present, although it has not been popularized on a large scale, but in our daily life In life, it is easy to see that HTML5 games, websites, and applications are emerging in endlessly. As a front-end staff, you should also know more about these API to lay the foundation for future applications. Below I will introduce to you the new HTML5. The introduced APIs, and the browser detection methods of each API, I mainly refer to the "html5 secrets" and "html5 advanced programming" that I read recently.

First, let's introduce Modernizr, which is. An open source Javascript Class Library used to detect browser support for HTML5 and CSS3 features. The latest version is version 2.5.3 (download). The method of use is very simple. On the page After introducing JS, it will run automatically and create a Modernizr global object , which creates a corresponding Boolean type attribute for each detectable feature. , we just need to call it, for example:

if( Modernizr.canvas ){  
// 恩,我知道这个属性,他是画图用的:)
}
else{  
// canvas 这是个什么东东??
}

1.CanvasCanvas is a resolution-dependent bitmap canvas, and the graphics it draws are not scalable , you can draw any graphics on Canvas through Javascript, and even load photos. The HTML5 standard has formulated a series of Canvas APIs for drawing simple graphics, defining paths, creating gradients, and applying image transformations. A Canvas is a rectangular area. By default, it is 300 pixels wide and 150 pixels high.
Note: The object drawn by Canvas does not belong to the page DOM structure or any other
namespace

// 创建一个 Canvas 元素,并检查该元素是否拥有 getContext() 方法,然后用双否定强制返回一个布尔值
var hasCanvas = !!document.createElement("canvas").getContext;
// Modernizr检测方法,返回布尔值
var hasCanvas = Modernizr.canvas ;

##2. The emergence of these two elements, .Audio and Video

, allows developers to play audio or video without using plug-ins. The HTML5 specification also provides a universal, complete, scriptable control API ##. #

// 创建一个 Audio 元素,并检查该元素是否拥有 canPlayType() 方法,然后用双否定强制返回一个布尔值
var hasAudio = !!document.createElement("audio").canPlayType;
// modernizr检测方法
var hasAudio = Modernizr.audio;
// 创建一个 Video 元素,并检查该元素是否拥有 canPlayType() 方法,然后用双否定强制返回一个布尔值
var hasVideo = !!document.createElement("video").canPlayType;
// modernizr检测方法
var hasVideo = Modernizr.video;
If you want to check whether the default format is supported, you can write:
var hasVideo = document.createElement("video").canPlayType('video/ogg; codecs="theora, vorbis"');
// modernizr检测方法
var hasVideo = Modernizr.video.ogg;

The native method will return "probably", "maybe" or "", which respectively means "completely confident to play this format", " "It is possible that this format can be played", "I am sure that this format cannot be played".

The parameter passed in by the canPlayType() method is expressed in text to ask the browser whether it can play the "theora" encoding format encapsulated in the ogg container. Video and audio in "vorbis" format


3.Web Storage

Web Storage (also known as DOMStorage) allows developers to store data in Javascript objects on the page. Saved on load and easily accessible. Developers can choose whether to activate this data when opening a new window or new tab and when restarting the browser. The stored data will not be transmitted over the network and can save big data up to several megabytes.

// 支持的话,全局 window 对象会有一个 localStorage 属性
var hasWebStorage = window.localStorage;
// modernizr检测方法
var hasWebStorage = Modernizr.localstorage;

4.Web WorkersWeb Workers can allow Web applications to have background processing capabilities, and it supports multi-threading very well. Therefore, Javascript applications using HTML5 can take full advantage of multi-core CPUs and allocate long-term tasks to Web Workers for execution.

Note: Scripts executed in Web Workers cannot access the window object of the page.

// 支持的话,全局 window 对象会有一个 Worker 属性
var hasWorker = window.Worker;
// modernizr检测方法
var hasWorker = Modernizr.webworkers;
5.Offline Web Applications

HTML5's offline application cache makes it possible to run applications without a network connection. When you first visit a Web site with offline access capabilities, the Web server tells the browser which files are necessary to ensure normal operation offline. These files can be any files-HTML, Javascript, images, or videos.

// 支持的话,全局 window 对象会有一个 applicationCache 属性
var hasApplicationCache = window.applicationCache;
// modernizr检测方法
var hasApplicationCache = Modernizr.applicationcache ;

6.Geolocation
HTML5’s geolocation API can locate where you are in the world and share the location information with permission. Many interesting applications can be built with this amazing feature. For example, calculating running distance, social applications based on GPS navigation, etc. It obtains positioning data through IP address, GPS geolocation, Wi-Fi geolocation, mobile phone geolocation, and custom geolocation.

// 支持的话,全局 navigator 对象会有一个 geolocation 属性
var hasGeolocation = navigator.geolocation;
// modernizr检测方法
var hasGeolocation = Modernizr.geolocation;

7.Forms
HTML5 defines many new input box types: search representing search, numeric type input box number, range selection slider range, color selector color, phone number input box tel, website address input box url, email input box email, date selector date, month input box month, week input box week, timestamp input box time, accurate date/time stamp entry and exit box datetime, local Date and time input box datetime-local.

// 创建一个 input 元素,该元素默认是 text 类型,改变他的类型,然后查看改变是否被保留
var o = document.createElement("input");o.setAttribute("type","color");return i.type != "text";
// modernizr检测方法
var hasInputType = Modernizr.inputtypes.color;

8.WebSockets
WebSockets 是 HTML5 中最强大的通信功能,它定义了一个全双工通信通道(又称为双向同时通信,即通信的双方可以同时发送和接受信息的信息交互方式),仅通过 Web 上的一个 Socket 即可进行通信。它不仅仅是对常规 HTTP 通信的另一种增量加强,更代表着一次巨大的进步,对实时的、事件驱动的程序而言更是如此。

// 支持的话,全局 window 对象会有一个 webSocket 属性
var hasWebSocket = window.webSocket;
// modernizr检测方法
var hasWebSocket = Modernizr.websockets;

8.Communication
Communication 是 HTML5 中用来实现正在运行的两个页面之间(iframe、标签页、窗口)进行跨源通信和信息共享的API。它把 postMessage API 定义为发送消息的标准方式。

// 支持的话,全局 window 对象会有一个 postMessage 属性
var hasPostMessage = window.postMessage;
// modernizr检测方法
var hasPostMessage = Modernizr.postmessage;

API 的浏览器支持情况主要介绍这些,以后我会逐个 API 进行详细讲解,把自己学到和研究的东西共享出来,欢迎大家来一块交流学习:)

The above is the detailed content of Detailed introduction to HTML5 API browser support detection. 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
H5: How It Enhances User Experience on the WebH5: How It Enhances User Experience on the WebApr 19, 2025 am 12:08 AM

H5 improves web user experience with multimedia support, offline storage and performance optimization. 1) Multimedia support: H5 and elements simplify development and improve user experience. 2) Offline storage: WebStorage and IndexedDB allow offline use to improve the experience. 3) Performance optimization: WebWorkers and elements optimize performance to reduce bandwidth consumption.

Deconstructing H5 Code: Tags, Elements, and AttributesDeconstructing H5 Code: Tags, Elements, and AttributesApr 18, 2025 am 12:06 AM

HTML5 code consists of tags, elements and attributes: 1. The tag defines the content type and is surrounded by angle brackets, such as. 2. Elements are composed of start tags, contents and end tags, such as contents. 3. Attributes define key-value pairs in the start tag, enhance functions, such as. These are the basic units for building web structure.

Understanding H5 Code: The Fundamentals of HTML5Understanding H5 Code: The Fundamentals of HTML5Apr 17, 2025 am 12:08 AM

HTML5 is a key technology for building modern web pages, providing many new elements and features. 1. HTML5 introduces semantic elements such as, , etc., which enhances web page structure and SEO. 2. Support multimedia elements and embed media without plug-ins. 3. Forms enhance new input types and verification properties, simplifying the verification process. 4. Offer offline and local storage functions to improve web page performance and user experience.

H5 Code: Best Practices for Web DevelopersH5 Code: Best Practices for Web DevelopersApr 16, 2025 am 12:14 AM

Best practices for H5 code include: 1. Use correct DOCTYPE declarations and character encoding; 2. Use semantic tags; 3. Reduce HTTP requests; 4. Use asynchronous loading; 5. Optimize images. These practices can improve the efficiency, maintainability and user experience of web pages.

H5: The Evolution of Web Standards and TechnologiesH5: The Evolution of Web Standards and TechnologiesApr 15, 2025 am 12:12 AM

Web standards and technologies have evolved from HTML4, CSS2 and simple JavaScript to date and have undergone significant developments. 1) HTML5 introduces APIs such as Canvas and WebStorage, which enhances the complexity and interactivity of web applications. 2) CSS3 adds animation and transition functions to make the page more effective. 3) JavaScript improves development efficiency and code readability through modern syntax of Node.js and ES6, such as arrow functions and classes. These changes have promoted the development of performance optimization and best practices of web applications.

Is H5 a Shorthand for HTML5? Exploring the DetailsIs H5 a Shorthand for HTML5? Exploring the DetailsApr 14, 2025 am 12:05 AM

H5 is not just the abbreviation of HTML5, it represents a wider modern web development technology ecosystem: 1. H5 includes HTML5, CSS3, JavaScript and related APIs and technologies; 2. It provides a richer, interactive and smooth user experience, and can run seamlessly on multiple devices; 3. Using the H5 technology stack, you can create responsive web pages and complex interactive functions.

H5 and HTML5: Commonly Used Terms in Web DevelopmentH5 and HTML5: Commonly Used Terms in Web DevelopmentApr 13, 2025 am 12:01 AM

H5 and HTML5 refer to the same thing, namely HTML5. HTML5 is the fifth version of HTML, bringing new features such as semantic tags, multimedia support, canvas and graphics, offline storage and local storage, improving the expressiveness and interactivity of web pages.

What Does H5 Refer To? Exploring the ContextWhat Does H5 Refer To? Exploring the ContextApr 12, 2025 am 12:03 AM

H5referstoHTML5,apivotaltechnologyinwebdevelopment.1)HTML5introducesnewelementsandAPIsforrich,dynamicwebapplications.2)Itsupportsmultimediawithoutplugins,enhancinguserexperienceacrossdevices.3)SemanticelementsimprovecontentstructureandSEO.4)H5'srespo

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

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.