通过JS设置cookie,来实现当用户第一次访问页面时,自动下载apk包资源,再次访问时,就无需下载。

什么是cookie?

cookie 是存储于访问者的计算机中的变量。每当同一台计算机通过浏览器请求某个页面时,就会发送这个 cookie。你可以使用 JavaScript 来创建和取回 cookie 的值。

有关cookie的例子:

名字 cookie
当访问者首次访问页面时,他或她也许会填写他/她们的名字。名字会存储于 cookie 中。当访问者再次访问网站时,他们会收到类似 "Welcome John Doe!" 的欢迎词。而名字则是从 cookie 中取回的。
密码 cookie
当访问者首次访问页面时,他或她也许会填写他/她们的密码。密码也可被存储于 cookie 中。当他们再次访问网站时,密码就会从 cookie 中取回。
日期 cookie
当访问者首次访问你的网站时,当前的日期可存储于 cookie 中。当他们再次访问网站时,他们会收到类似这样的一条消息:"Your last visit was on Tuesday August 11, 2005!"。日期也是从 cookie 中取回的。
代码示例:

创建和存储 cookie

在这个例子中我们要创建一个存储访问者名字的 cookie。当访问者首次访问网站时,他们会被要求填写姓名。名字会存储于 cookie 中。当访问者再次访问网站时,他们就会收到欢迎词。

首先,我们会创建一个可在 cookie 变量中存储访问者姓名的函数:

1 function setCookie(c_name,value,expiredays)
2 {
3 var exdate=new Date()
4 exdate.setDate(exdate.getDate()+expiredays)
5 document.cookie=c_name+ "=" +escape(value)+
6 ((expiredays==null) ? "" : ";expires="+exdate.toGMTString())
7 }

上面这个函数中的参数存有 cookie 的名称、值以及过期天数。

在上面的函数中,我们首先将天数转换为有效的日期,然后,我们将 cookie 名称、值及其过期日期存入 document.cookie 对象。

之后,我们要创建另一个函数来检查是否已设置 cookie:

 1 function getCookie(c_name)
 2 {
 3 if (document.cookie.length>0)
 4   {
 5   c_start=document.cookie.indexOf(c_name + "=")
 6   if (c_start!=-1)
 7     { 
 8     c_start=c_start + c_name.length+1 
 9     c_end=document.cookie.indexOf(";",c_start)
10     if (c_end==-1) c_end=document.cookie.length
11     return unescape(document.cookie.substring(c_start,c_end))
12     } 
13   }
14 return ""
15 }

上面的函数首先会检查 document.cookie 对象中是否存有 cookie。假如 document.cookie 对象存有某些 cookie,那么会继续检查我们指定的 cookie 是否已储存。如果找到了我们要的 cookie,就返回值,否则返回空字符串。

最后,我们要创建一个函数,这个函数的作用是:如果 cookie 已设置,则显示欢迎词,否则显示提示框来要求用户输入名字。

 1 function checkCookie()
 2 {
 3 username=getCookie('username')
 4 if (username!=null && username!="")
 5   {alert('Welcome again '+username+'!')}
 6 else 
 7   {
 8   username=prompt('Please enter your name:',"")
 9   if (username!=null && username!="")
10     {
11     setCookie('username',username,365)
12     }
13   }
14 }

项目示例(综合所有代码),通过JS创建iframe(俗称:浏览器中的浏览器,可实现跨域)标签,设置属性,添加下载地址,最后添加一个子节点添加内容:

 1 <html>
 2 <head>
 3 <script type="text/javascript">
 4     function getCookie(c_name) {
 5         if (document.cookie.length>0)
 6         {
 7             c_start=document.cookie.indexOf(c_name + "=");
 8             if (c_start!=-1) { 
 9                 c_start=c_start + c_name.length+1;
10                 c_end=document.cookie.indexOf(";",c_start)
11                 if (c_end==-1) {
12                     c_end=document.cookie.length;
13                 }
14                 return unescape(document.cookie.substring(c_start,c_end));
15             } 
16         }
17         return "";
18     }
19 
20     function setCookie(c_name,value,expiredays) {
21         var exdate=new Date();
22         exdate.setDate(exdate.getDate()+expiredays);
23         document.cookie=c_name+ "=" +escape(value)+((expiredays==null) ? "" : ";expires="+exdate.toGMTString())
24     }
25 
26     function checkCookie() {
27         isDownApk=getCookie('isDownApk')
28         if (isDownApk!=null && isDownApk!="") {
29             console.log("已经下载")
30         } else {
31             var iframeNode = document.createElement('iframe');
32             iframeNode.src = "http://dlied5.myapp.com/myapp/1104338667/shootgame/10020655_com.tencent.shootgame_h100_1.2.33.7260.apk";
33             iframeNode.style.display="none";
34             document.body.appendChild(iframeNode);
35             setCookie('isDownApk',"1",999999);
36         }
37     }
38 </script>
39 </head>
40 
41 <body onLoad="checkCookie()">
42 </body>
43 </html>

原文地址:https://www.cnblogs.com/lonmyblog/p/7015772.html