js中的cookie的设置

 1 function SetCookie(name, value) {
 2     var exp = new Date();
 3     exp.setTime(exp.getTime() + 3600000000); //设置他的过期的时间
 4     document.cookie = name + "=" + value + "; expires=" + exp.toGMTString() + "; path=/";
 5 }
 6 function getCookieval_r(offset) {
 7     var endstr = document.cookie.indexOf(";", offset);
 8     if (endstr == -1) endstr = document.cookie.length;
 9     return unescape(document.cookie.substring(offset, endstr));
10 }
11 function DelCookie(name) {
12     var exp = new Date();
13     exp.setTime(exp.getTime() - 1);
14     var cval = GetCookie(name);
15     document.cookie = name + "=" + '' + "; expires=" + exp.toGMTString() + "; path=/"; 如果想要删除要和设置的时候的路径完全一致
16    
17 }
18 function GetCookie(name) {
19     var arg = name + "=";
20     var alen = arg.length;
21     var clen = document.cookie.length;
22     var i = 0;
23     while (i < clen) {
24         var j = i + alen;
25         if (document.cookie.substring(i, j) == arg) return getCookieval_r(j);
26         i = document.cookie.indexOf(" ", i) + 1;
27         if (i == 0) break;
28     }
29     
30     return null;
31 }

 

posted @ 2015-08-04 10:35  杰骜不驯  阅读(421)  评论(0编辑  收藏  举报