原生js cookie的使用实例setCookie()、getCookie()方法

01  <html>
02  <head>
03  <title>原生js cookie的使用实例setCookie()、getCookie()方法 </title>
04  <script  language= "javascript" >
05  <!--
06  function   setCookie (){
07      var   never   =   new   Date();
08      //设置never的时间为当前时间加上十年的毫秒值
09      never . setTime( never . getTime() + 10 * 365 * 24 * 60 * 60 * 1000);     
10      var   expString   =   "expires=" +   never . toGMTString() + ";";
11     
12      document . cookie   =   "area=" + escape( "北京海淀") + "; " + expString;
13      document . cookie   =   "zipcode=100080;";
14     
15  } // end function
16 
17 
20  function   getCookie( name ){
21      var   result   =   null;
22      //对cookie信息进行相应的处理,方便搜索
23      var   myCookie   =   "" + document . cookie + ";";  
24      var   searchName   =   "+name+" = ";
25     var startOfCookie = myCookie.indexOf(searchName);
26     var endOfCookie;
27     if(satrtOfCookie != -1){
28         startOfcookie += searchName.length;
29         endOfCookie = myCookie.indexOf("; " , startOfCookie);
30            result   =  ( myCookie . substring( startOfCookie , endOfCookie));
31      }
32      return   result;
33  }   //end function
34  //-->
35  </script>
36  </head>
37  <body>
38  <script>
39      document . write( document . cookie + "<br/>");
40      document . write( "area is" + getCookie( "area") + ",and zipcode is" + getCookie( "zipcode"));
41      document . write( "<br/>");
42  </script>
43  </body>
44  </html>
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值