CACHE MANIFEST#version 1.1CACHE:css/style.cssindex.htmlNETWORK:FALLBACK:



Detailed graphic and text explanation of html5 application caching and Web Workers
Introduced in html5 Applicationcaching, which means that web applications can be cached and accessed when there is no network connection
Advantages of application caching
Offline browsing, users can browse loaded and cached data while offline
Speed up loading speed. #Reduce server load
- ##Implement cachingIf you enable application caching, you need to include the manifest attribute in the
The manifest file provides us with three caching methods:
version n.n: version indicates the version of the current manifest. , when the version changes, when the user loads again, all files listed under the CACHE label will be downloaded again
- NETWORK: Files listed under this heading require a link to the server and will not be cached.
- FALL
BACK: The files listed under this title specify the resources to be accessed after the access cache fails. The first one is the access source, and the second one is the replacement file *.html /offline.html. For example
404 page
.
Cached applicationHere I create a web project and create a new html file: index.html
<!DOCTYPE html><html manifest="index.appcache"> <head> <title>index.html</title> <link rel="stylesheet" type="text/css" href="./css/style.css"> </head> <body> <h1 id="This-nbsp-is-nbsp-my-nbsp-HTML-nbsp-page">This is my HTML page</h1> </body></html>
style.css
@CHARSET "UTF-8";h1 { color: aqua;}
As you can see here, my page is very simple, and
references
index.appcache is as follows: CACHE MANIFEST#version 1.0CACHE:index.htmlcss/style.css
CACHE MANIFEST#version 1.0CACHE:index.htmlNETWORK:css/style.css
First you need to manually clear the previously cached records. Then open the server and browse http://localhost:8080/html5cache/index.html. The display effect is as follows:

You can see that only the html page is cached at this time, and the css/ is not cached. style.css file, close the server at this time, refresh the page again, the effect is as follows:
You can see that at this time, only the html page is loaded, and no css file is loaded, so the h1 tag The font is the default.
UpdateCache
CACHE MANIFEST#version 1.1CACHE:css/style.cssindex.htmlNETWORK:FALLBACK:
Copy after loginHere, I changed the version to 1.1. When I visit the page again, I will go to the service to download the latest page. At this point, the shortcomings of caching come out. Even if I only update one line of text on a page, when I change the version value in the "index.appcache" file, all the content defined in the CACHE will be downloaded again.
Use js to automatically update the cacheIn addition, we can also use the applicationcacheCACHE MANIFEST#version 1.1CACHE:css/style.cssindex.htmlNETWORK:FALLBACK:
object
to automatically update the cache. As follows:<script type="text/javascript">
//添加页面加载函数
window.addEventListener('load', function(e) {
//为applicationCache对象添加updateready事件
window.applicationCache.addEventListener('updateready', function(e) {
//表示manifest中列举的文件已经重新下载并更新成功
if (window.applicationCache.status == window.applicationCache.UPDATEREADY) {
//使用swapCache()方法更新到应用程序中
window.applicationCache.swapCache();
if (confirm('A new version of this site is available. Load it?')) {
//重新加载当前页面
window.location.reload();
}
} else { //manifest文件没有变化
console.log("manifest 没有改变");
}
}, false);
}, false); </script>
Copy after login
applicationcache is a direct sub-object of the <script type="text/javascript"> //添加页面加载函数 window.addEventListener('load', function(e) { //为applicationCache对象添加updateready事件 window.applicationCache.addEventListener('updateready', function(e) { //表示manifest中列举的文件已经重新下载并更新成功 if (window.applicationCache.status == window.applicationCache.UPDATEREADY) { //使用swapCache()方法更新到应用程序中 window.applicationCache.swapCache(); if (confirm('A new version of this site is available. Load it?')) { //重新加载当前页面 window.location.reload(); } } else { //manifest文件没有变化 console.log("manifest 没有改变"); } }, false); }, false); </script>
window object. The event list of this object is as follows:
status returns the cached state
可选值 | 匹配常量 | 描述 |
---|---|---|
0 | appCache.UNCACHED | 未缓存 |
1 | appCache.IDLE | 闲置 |
2 | appCache.CHECKING | 检查中 |
3 | appCache.DOWNLOADING | 下载中 |
4 | appCache.UPDATEREADY | 已更新 |
5 | appCache.OBSOLETE | 失效 |
方法
方法名 | 匹配常量 |
---|---|
update() | 发起应用程序缓存下载进程 |
abort() | 取消正在进行的缓存下载 |
swapcache() | 切换成本地最新的缓存环境 |
web workers
web workers是运行在后台的脚本,独立于其他的脚本,不会影响页面的性能。类似于android开发中的handler。将繁重耗时的计算放到web worker中来实现,然后将处理的结果返回给主UI线程来显示。
web workers方法
postMessage() :用于向html页面回传一段消息。
terminate() :终止web workers,并且释放计算机资源。
web workers简单实现
下面使用web workers简单实现一数字更新的demo:
新建一个web工程,创建index.html
<!DOCTYPE html><html><head lang="en"> <meta charset="UTF-8"> <title></title> <script src="index.js"></script></head><body> <p id="nump">0</p></body></html>
这里在index.html文件当中引入了index.js文件。
index.js
var nump; window.onload = function(){ nump = document.getElementById("nump"); var work = new Worker("count.js"); work.onmessage = function(e) { //alert(e.data); nump.innerHTML = e.data; }; };
可以看到,这里讲更新数据的具体操作,使用Worker来更新,在worker当中加载了count.js文件来做一些耗时,复杂的计算。然后使用worker的onmessage回调方法,将count.js返回的结果重新显示给nump。
count.js
var countNum = 0;function count(){ postMessage(countNum);//通过postMessage方法将计算结果回传给调用者 countNum++; setTimeout(count,1000); } count();
count.js文件比较简单,每隔一秒更新countNum的值,然后回传给调用者,也就是这里的index.js
此时运行效果如下:
下面添加一个开始停止的控制按钮:
<button id="start">start</button> <button id="stop">stop</button>
index.js
var nump;var work; window.onload = function(){ nump = document.getElementById("nump"); var start = document.getElementById("start"); var stop = document.getElementById("stop"); start.onclick = startWorker; stop.onclick = stopWorker; };function startWorker() { if (work) { //如果work存在,则直接返回 return; } else { work = new Worker("count.js"); work.onmessage = function(e) { nump.innerHTML = e.data; }; } }function stopWorker() { if (work) {//如果worker存在,则终止并且为其重新赋值 work.terminate(); work = null; } }
此时运行效果如下:
另外我们还可以通过navaigator对象的onLine属性来判断当前浏览器是否在线,该属性属于只读属性,会返回boolean类型的值。
if(window.navigator.onLine) { //在线} else { //离线}
The above is the detailed content of Detailed graphic and text explanation of html5 application caching and Web Workers. For more information, please follow other related articles on the PHP Chinese website!

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

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Guide to Table Border in HTML. Here we discuss multiple ways for defining table-border with examples of the Table Border in HTML.

This is a guide to Nested Table in HTML. Here we discuss how to create a table within the table along with the respective examples.

Guide to HTML margin-left. Here we discuss a brief overview on HTML margin-left and its Examples along with its Code Implementation.

Guide to HTML Table Layout. Here we discuss the Values of HTML Table Layout along with the examples and outputs n detail.

Guide to HTML Input Placeholder. Here we discuss the Examples of HTML Input Placeholder along with the codes and outputs.

Guide to the HTML Ordered List. Here we also discuss introduction of HTML Ordered list and types along with their example respectively

Guide to Moving Text in HTML. Here we discuss an introduction, how marquee tag work with syntax and examples to implement.

Guide to HTML onclick Button. Here we discuss their introduction, working, examples and onclick Event in various events respectively.
