JS设置cookie,读取cookie,删除cookie

总结了一下cookie的使用,不全面.都是基础的知识,后期还会再添加.

 1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 2 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
 3 <head>
 4   <meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
 5   <title>cookie</title>
 6 </head>
 7 <body>
 8   
 9 </body>
10 </html>
11 <script type="text/javascript">
12 //write cookie
13 function setCookie(name, value){
14       var Days=30;
15       var exp=new Date();
16       exp.setTime(exp.getTime()+Days*24*60*60*1000);
17       document.cookie=name+"="+escape(value)+";expires="+exp.toGMTString();
18 }
19 //reading cookie
20 function getCookie(){
21         var arr,reg=new RegExp("(^|)"+name+"([^;]*)(;|$)");
22         if(arr=document.cookie.match(reg)){
23                       return unescape(arr[2]); 
24         }else{
25                       return null;
26         }
27 }
28 //delete cookie
29 function delCookie(name){
30         var exp=new Date();
31         exp.setTime(exp.getTime()-1);
32         var cval=getCookie(name);
33         if(cval!=null){
34             document.cookie=name+"="+cval+";expires="+exp.toGMTString();
35         }
36 }
37 setCookie("name","liubeimeng");
38 setCookie("name1","liubeimeng1");
39 delCookie("name1");
40 alert(getCookie("name")); 
41 </script>

你可能感兴趣的:(JS设置cookie,读取cookie,删除cookie)