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 }