---
title: "构建web离线应用"
author: "cakin24"
date: "2017-04-23T08:59:58Z"
tags: []
categories: ["编程语言"]
slug: "web"
draft: false
lastmod: "2017-04-23T08:59:58Z"
id: 2370835
url: "https://cakin24.iteye.com/blog/2370835"
---

**一 构建步骤**

1、搭建web服务器

2、给&lt;Html&gt;元素增加manifest属性

3、编辑manifest文件。

 

**二 代码**

核心代码：

1、HTML文件（index.html）

01. `<!DOCTYPE html>`
02. `<htmlmanifest="index.manifest">`
03. `<head>`
04. `<metaname="author"content="Yeeku.H.Lee(CrazyIt.org)"/>`
05. `<metahttp-equiv="Content-Type"content="text/html; charset=GBK"/>`
06. `<title> 测试页面 </title>`
07. `<scripttype="text/javascript"src="test.js">`
08. `</script>`
09. `</head>`
10. `<body>`
11. `<imgsrc="logo.jpg"alt="疯狂Java联盟"/>`
12. `<inputid="bn"type="button"value="单击"/>`
13. `</body>`
14. `</html>`

2、manifest文件（index.manifest）

01. `CACHE MANIFEST`
02. `#该文件的第一行必须是CACHE MANIFEST`
03. `#下面指定该清单文件的版本号`
04. `#version 1`
05. `#CACHE:后面列出的是需要缓存的资源`
06. `CACHE:`
07. `index.html`
08. `logo.jpg`
09. `#NETWORK:后面列出的不进行缓存的资源`
10. `NETWORK:`
11. `*`
12. `#FALLBACK:后面每行需要列出两个资源。`
13. `#第一个资源是处于在线状态时使用的资源。`
14. `#第二个资源是处于离线状态时所使用的资源。`
15. `FALLBACK:`
16. `test.js offline.js`

3、JS文件（test.js和offline.js）

tet.js代码如下：

1. `window.onload =function()`
2. `{`
3. `document.getElementById("bn").onclick =function()`
4. `{`
5. `alert("您单击了按钮！");`
6. `};`
7. `};`

office.js代码：

1. `window.onload =function()`
2. `{`
3. `document.getElementById("bn").onclick =function()`
4. `{`
5. `alert("您处于离线状态，您单击了按钮！");`
6. `};`
7. `};`

 

**三 运行结果**

服务器在启动状态和在关闭状态下的运行结果如下：

![](http://dl2.iteye.com/upload/attachment/0124/5088/e66f84b1-e9b0-31d3-92f2-c8de79b3c20c.png)