Home > Article > Web Front-end > Sample code for creating offline web applications in html5
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>您要的页面找不到了!</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 id="imgtarget" src="banana100.png"/> <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 id="imgtarget" src="banana100.png"/> <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!