cookie的使用

名字 cookie

当访问者首次访问页面时,他或她也许会填写他/她们的名字。名字会存储于 cookie 中。当访问者再次访问网站时,他们会收到类似 "Welcome John Doe!" 的欢迎词。而名字则是从 cookie 中取回的。

密码 cookie

当访问者首次访问页面时,他或她也许会填写他/她们的密码。密码也可被存储于 cookie 中。当他们再次访问网站时,密码就会从 cookie 中取回。

日期 cookie

当访问者首次访问你的网站时,当前的日期可存储于 cookie 中。当他们再次访问网站时,他们会收到类似这样的一条消息:"Your last visit was on Tuesday August 11, 2005!"。日期也是从 cookie 中取回的。
 1 //创建和存储 cookie
 2             function getCookie(c_name) {
 3                 if(document.cookie.length > 0) {
 4                     c_start = document.cookie.indexOf(c_name + "=")
 5                     if(c_start != -1) {
 6                         c_start = c_start + c_name.length + 1
 7                         c_end = document.cookie.indexOf(";", c_start)
 8                         if(c_end == -1) c_end = document.cookie.length
 9                         return unescape(document.cookie.substring(c_start, c_end))
10                     }
11                 }
12                 return ""
13             }
14             
15             function setCookie(c_name, value, expiredays) {
16                 var exdate = new Date()
17                 exdate.setDate(exdate.getDate() + expiredays)
18                 document.cookie = c_name + "=" + escape(value) +
19                     ((expiredays == null) ? "" : ";expires=" + exdate.toGMTString())
20             }
21 
22             function checkCookie() {
23                 username = getCookie('username')
24                 if(username != null && username != "") {
25                     alert('Welcome again ' + username + '!')
26                 } else {
27                     username = prompt('Please enter your name:', "")
28                     if(username != null && username != "") {
29                         setCookie('username', username, 365)
30                     }
31                 }
32             }
原文地址:https://www.cnblogs.com/hgs-159/p/6795246.html