无明居

  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  10 随笔 :: 0 文章 :: 1 评论 :: 0 Trackbacks
  1 //  utility function to retrieve an expiration date in proper
  2
  3 //  format; pass three integer parameters for the number of days, hours,
  4
  5 //  and minutes from now you want the cookie to expire (or negative
  6
  7 //  values for a past date); all three parameters are required,
  8
  9 //  so use zeros where appropriate
 10
 11 function  getExpDate(days, hours, minutes)  {
 12
 13      var  expDate  =   new  Date( );
 14
 15      if  ( typeof  days  ==   " number "   &&   typeof  hours  ==   " number "   &&  
 16
 17          typeof  hours  ==   " number " {
 18
 19         expDate.setDate(expDate.getDate( )  +  parseInt(days));
 20
 21         expDate.setHours(expDate.getHours( )  +  parseInt(hours));
 22
 23         expDate.setMinutes(expDate.getMinutes( )  +  parseInt(minutes));
 24
 25          return  expDate.toGMTString( );
 26
 27     }

 28
 29 }

 30
 31    
 32
 33 //  utility function called by getCookie( )
 34
 35 function  getCookieVal(offset)  {
 36
 37      var  endstr  =  document.cookie.indexOf ( " ; " , offset);
 38
 39      if  (endstr  ==   - 1 {
 40
 41         endstr  =  document.cookie.length;
 42
 43     }

 44
 45      return  unescape(document.cookie.substring(offset, endstr));
 46
 47 }

 48
 49    
 50
 51 //  primary function to retrieve cookie by name
 52
 53 function  getCookie(name)  {
 54
 55      var  arg  =  name  +   " = " ;
 56
 57      var  alen  =  arg.length;
 58
 59      var  clen  =  document.cookie.length;
 60
 61      var  i  =   0 ;
 62
 63      while  (i  <  clen)  {
 64
 65          var  j  =  i  +  alen;
 66
 67          if  (document.cookie.substring(i, j)  ==  arg)  {
 68
 69              return  getCookieVal(j);
 70
 71         }

 72
 73         i  =  document.cookie.indexOf( "   " , i)  +   1 ;
 74
 75          if  (i  ==   0 break
 76
 77     }

 78
 79      return   "" ;
 80
 81 }

 82
 83    
 84
 85 //  store cookie value with optional details as needed
 86
 87 function  setCookie(name, value, expires, path, domain, secure)  {
 88
 89     document.cookie  =  name  +   " = "   +  escape (value)  +
 90
 91         ((expires)  ?   " ; expires= "   +  expires :  "" +
 92
 93         ((path)  ?   " ; path= "   +  path :  "" +
 94
 95         ((domain)  ?   " ; domain= "   +  domain :  "" +
 96
 97         ((secure)  ?   " ; secure "  :  "" );
 98
 99 }

100
101    
102
103 //  remove the cookie by setting ancient expiration date
104
105 function  deleteCookie(name,path,domain)  {
106
107      if  (getCookie(name))  {
108
109         document.cookie  =  name  +   " = "   +
110
111             ((path)  ?   " ; path= "   +  path :  "" +
112
113             ((domain)  ?   " ; domain= "   +  domain :  "" +
114
115              " ; expires=Thu, 01-Jan-70 00:00:01 GMT " ;
116
117     }

118
119 }

120
121
posted on 2006-07-04 22:52 无明 阅读(245) 评论(0)  编辑  收藏 所属分类: 收藏夹

只有注册用户登录后才能发表评论。


网站导航: