Javascript 读写 Cookie 操作 By shawl.qiu

Javascript 读写 Cookie 操作 By shawl.qiu


说明:
Javascript 的 Cookie 真是有够弱的, 让你写 Cookie 值, 比如:
  1. linenum
  2. value=this::value1=this 

但却不能让你直接读 value or value1.
让你 设置有效期限, 但只能使用 time.toGMTString() 进行设置, 其他时间格式统统无效. 

值内容还不能有空格呀, 分号呀, 等等, 因此 escape && unescape 大概是免不了的. 

Javascript 的 Cookie 主要有以下几个设置属性:
value = 设置值;
expires = 设置有效期限;
path = 设置作用路径;
domain = 设置作用域;
secure = 设置安全属性;

目录:
1. Javascript 写 Cookie 函数
2. Javascript 读 Cookie 函数

shawl.qiu 
2006-10-31
http://blog.csdn.net/btbtd

1. Javascript 写 Cookie 函数
  1. linenum
  2. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  3. <script type="text/javascript">
  4. //<![CDATA[
  5.     var timer=new Date();
  6.         timer.setSeconds(timer.getSeconds()+5000); 
  7.         
  8.     fWriteCookie(document.cookie, /* 读取原有 Cookie 值进行判断 */
  9.                 true, /* 是否覆盖原有的 Cookie 值  */
  10.                 /* 赋植给 Cookie, 请注意正确设置分隔符 */
  11.                 'title=test this::subtitle=test this too::novalue::hasvalue=ok',  
  12.                 true, /* 是否对 Cookie值 进行编码 */
  13.                 '##', /* Cookie 内容识别符 */
  14.                 '::', /* Cookie 内容分隔符 */
  15.                 timer.toGMTString(), /* 设置 Cookie 有效时间, 时间必须为 dateObj.toGMTString() 格式. */
  16.                 '/', /* 设置 Cookie 作用路径 */
  17.                 '', /* 设置 Cookie 作用 域名, 注意: 不能跨域, 子域除外. */
  18.                 false /* 是否使用 SSI 协议 */
  19.                 );
  20.  
  21.     /*-----------------------------------------------------------*/
  22.      * Javascript 写 Cookie 函数 By shawl.qiu
  23.      *-----------------------------
  24.      * 参数说明:
  25.      * cookie: 读取 Cookie 值
  26.      * overwrite: 是否覆盖原有 Cookie
  27.      * value: 作为 Cookie 值 写入 Cookie
  28.      * encode: 是否对 Cookie 值进行编码
  29.      * identifier: Cookie 值 识别符
  30.      * separate: Cookie 值 分隔符
  31.      * expires: Cookie 有效时限
  32.      * path: Cookie 起作用路径
  33.      * domain: Cookie 起作用域, 不能跨域, 子域除外
  34.      * secure: 是否使用 SSI 协议
  35.     /*-----------------------------------------------------------*/
  36.     //----------------Begin function fWriteCookie-----------------//
  37.     function fWriteCookie(cookie, overwrite, value, encode, identifier, separate, expires, path, domain, secure){
  38.         if(cookie.length>0&&!overwrite)return false;
  39.         if(!value)return false;
  40.         var temp='';
  41.         
  42.         var array=value.split(separate);
  43.         for(var i=0; i<array.length; i++){ // 删除没有 = 号的值
  44.             if(array[i].indexOf('=')==-1)array.splice(i,1);
  45.         }
  46.         
  47.         if(encode){ // 对 cookie 的值进行编码
  48.             for(var i=0; i<array.length; i++){ 
  49.                 temp=array[i].split('=');
  50.                 array[i]=temp[0]+'='+escape(temp[1]);
  51.             }
  52.         }
  53.         
  54.         cookie=identifier+(array.join(separate))+identifier
  55.         
  56.         if(expires!=undefined) cookie+=';expires='+expires;
  57.         if(path!=undefined) cookie+=';path='+path;
  58.         if(domain!=undefined) if(domain!=''){cookie+=';domain='+domain;};
  59.         if(secure!=undefined) if(secure){cookie+=';secure='+secure;};
  60.         cookie+=';'
  61.         
  62.         document.cookie=cookie;
  63.     } // shawl.qiu script
  64.     //----------------End function fWriteCookie-------------------//
  65. //]]>
  66. </script>

2. Javascript 读 Cookie 函数
  1. linenum
  2. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  3. <script type="text/javascript">
  4. //<![CDATA[
  5.     
  6.     var title=subtitle=hasvalue='';
  7.     
  8.         fReadCookie(document.cookie, '##', '::', true);
  9.         
  10.         document.write('<p/>');
  11.         document.write('title: ',title,'<br/>');
  12.         document.write('subtitle: ',subtitle,'<br/>');
  13.         document.write('hasvalue: ',hasvalue,'<br/>');
  14.     /*-----------------------------------------------------------*/
  15.      * Javascript 读取 Cookie 内容, 并动态赋值 函数 By shawl.qiu
  16.      *-----------------------------
  17.      * 参数说明:
  18.      * cookie: 读取 Cookie 值
  19.      * identifier: Cookie 值 识别符
  20.      * separate: Cookie 值 分隔符
  21.      * decode: 是否对 Cookie 值进行解码
  22.     /*-----------------------------------------------------------*/
  23.     //----------------Begin function fReadCookie-----------------//
  24.     function fReadCookie(cookie, identifier, separate, decode){
  25.         if(cookie.length==0)return false;
  26.         cookie=cookie.substring(cookie.indexOf(identifier)+2, cookie.lastIndexOf(identifier));
  27.         
  28.         var array=cookie.split(separate);
  29.         var temp='';
  30.         for(var i=0; i<array.length; i++){ // 跳过没有 = 号的值
  31.             if(array[i].indexOf('=')==-1)continue;
  32.             
  33.             temp=array[i].split('=');
  34.             if(decode!=undefined){if(decode){temp[1]=unescape(temp[1]);}}
  35.             temp[0]=temp[0].replace(/^[/s]+|[/s]+$/,'');
  36.             temp[1]=temp[1].replace(/^[/s]+|[/s]+$/,'');
  37.             eval(temp[0]+"=temp[1]");
  38.         }        
  39.     } // shawl.qiu script
  40.     //----------------End function fReadCookie-------------------//
  41. //]]>
  42. </script>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值