Offline ApplicationCacheThe feature allows us to specify all the resources required by the web application,
This way the browser can download them all when loading the HTML document.
1) Define browser cache:
Enable offline caching - create a manifest file and reference in the manifest attribute of the html element It;
#Specifies the resources to be cached in the offline application - list the resources at the top of the manifest file or in the CACHE area;
Specify the backup content to be displayed when the resource is unavailable - list the content in the FALLBACK area of the manifest file;
Points to resources that are always requested from the server - list the contents in the BETWORK area of the manifest file;
Example: First create the manifest file of fruit.appcache
##
CACHE MANIFEST example.html banana100.png FALLBACK: * 404.html NETWORK: cherries100.png CACHE: apple100.png
Create the 404.html file again. If the html file pointed to by the link is not in the offline cache, you can use it instead.
<!DOCTYPE HTML> <html manifest="fruit.appcache"> <head> <title>Offline</title> </head> <body> <h1 id="您要的页面找不到了">您要的页面找不到了!</h1> 或许您可以帮我们找找孩子! </body> </html>
Finally create the html file that needs to be enabled for offline caching
<!DOCTYPE HTML> <html manifest="fruit.appcache"> <head> <title>Example</title> <style> img {border: medium double black; padding: 5px; margin: 5px;} </style> </head> <body> <img src="/static/imghwm/default1.png" data-src="banana100.png" class="lazy" id="imgtarget" / alt="Sample code for creating offline web applications in html5" > <p> <button id="banana">Banana</button> <button id="apple">Apple</button> <button id="cherries">Cherries</button> </p> <a href="otherpage.html">Link to another page</a> <script> var buttons = document.getElementsByTagName("button"); for (var i = 0; i < buttons.length; i++) { buttons[i].onclick = handleButtonPress; } function handleButtonPress(e) { document.getElementById("imgtarget").src = e.target.id + "100.png"; } </script> </body> </html>
2) Detect browserStatus:
window.navigator.online——If the browser is determined to be offline, return false, if the browser may be online, return true;
3) Use offline cache:
You can call window. The applicationCache attribute directly uses the offline cache, which will return an ApplicationCache object;
ApplicationCache objectMembers:
update()——Update cache to ensure that all items in the list have downloaded the latest version;
swapCache()——Swap the current cache with the newer cache;
status ——Return cache status;
3.1) ApplicationCache object The status attribute value:
0——UNCACHED——This document is not cached, or cached data has not been downloaded;
1——IDLE——The cache did not perform any operation;
2——CHECKING——浏览器正在检查清单或清单所指定项目的更新;
3——DOWNLOADING——浏览器正在下载清单或内容的更新;
4——UPDATEREADY——有更新后的缓存数据可用;
5——OBSOLETE——缓存数据已经废弃,不应该再使用了。这是请求清单文件时返回HTTP状态码4xx所造成的
(通常表明清单文件已被移走/删除);
3.2)ApplicationCache对象定义的事件,在缓存状态改变时触发:
checking——浏览器正在获取初始清单或者检查清单更新;
noupdate——没有更新可用,当前的清单是最新版;
downloading——浏览器正在下载清单里指定的内容;
progress——在下载阶段中触发;
cached——清单里指定的所有内容都已被下载和缓存了;
updateready——新资源已下载并且可以使用了;
obsolete——缓存已废弃;
CACHE MANIFEST CACHE: example.html banana100.png cherries100.png apple100.png FALLBACK: * offline2.html
<!DOCTYPE HTML> <html manifest="fruit.appcache"> <head> <title>Example</title> <style> img {border: medium double black; padding: 5px; margin: 5px;} p {margin-top: 10px; margin-bottom: 10px} table {margin: 10px; border-collapse: collapse;} th, td {padding: 2px;} body > * {float: left;} </style> </head> <body> <p> <img src="/static/imghwm/default1.png" data-src="banana100.png" class="lazy" id="imgtarget" / alt="Sample code for creating offline web applications in html5" > <p> <button id="banana">Banana</button> <button id="apple">Apple</button> <button id="cherries">Cherries</button> </p> <p> <button id="update">Update</button> <button id="swap">Swap Cache</button> </p> The status is: <span id="status"></span> </p> <table id="eventtable" border="1"> <tr><th>Event Type</th></tr> </table> <script> var buttons = document.getElementsByTagName("button"); for (var i = 0; i < buttons.length; i++) { buttons[i].onclick = handleButtonPress; } window.applicationCache.onchecking = handleEvent; window.applicationCache.onnoupdate = handleEvent; window.applicationCache.ondownloading = handleEvent; window.applicationCache.onupdateready = handleEvent; window.applicationCache.oncached = handleEvent; window.applicationCache.onobselete = handleEvent; function handleEvent(e) { document.getElementById("eventtable").innerHTML += "<tr><td>" + e.type + "</td></td>"; checkStatus(); } function handleButtonPress(e) { switch (e.target.id) { case 'swap': window.applicationCache.swapCache(); break; case 'update': window.applicationCache.update(); checkStatus(); break; default: document.getElementById("imgtarget").src = e.target.id + "100.png"; } } function checkStatus() { var statusNames = ["UNCACHED", "IDLE", "CHECKING", "DOWNLOADING", "UPDATEREADY", "OBSOLETE"]; var status = window.applicationCache.status; document.getElementById("status").innerHTML = statusNames[status]; } </script> </body> </html>
The above is the detailed content of Sample code for creating offline web applications in html5. For more information, please follow other related articles on the PHP Chinese website!

There is no difference between HTML5 and H5, which is the abbreviation of HTML5. 1.HTML5 is the fifth version of HTML, which enhances the multimedia and interactive functions of web pages. 2.H5 is often used to refer to HTML5-based mobile web pages or applications, and is suitable for various mobile devices.

HTML5 is the latest version of the Hypertext Markup Language, standardized by W3C. HTML5 introduces new semantic tags, multimedia support and form enhancements, improving web structure, user experience and SEO effects. HTML5 introduces new semantic tags, such as, ,, etc., to make the web page structure clearer and the SEO effect better. HTML5 supports multimedia elements and no third-party plug-ins are required, improving user experience and loading speed. HTML5 enhances form functions and introduces new input types such as, etc., which improves user experience and form verification efficiency.

How to write clean and efficient HTML5 code? The answer is to avoid common mistakes by semanticizing tags, structured code, performance optimization and avoiding common mistakes. 1. Use semantic tags such as, etc. to improve code readability and SEO effect. 2. Keep the code structured and readable, using appropriate indentation and comments. 3. Optimize performance by reducing unnecessary tags, using CDN and compressing code. 4. Avoid common mistakes, such as the tag not closed, and ensure the validity of the code.

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.

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.

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.

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.

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.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

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.

WebStorm Mac version
Useful JavaScript development tools

Atom editor mac version download
The most popular open source editor

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

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