现在的位置: 首页 > 综合 > 正文

Javascript 读写 Cookie 操作 By shawl.qiu

2012年12月06日 ⁄ 综合 ⁄ 共 4577字 ⁄ 字号 评论关闭

Javascript 读写 Cookie 操作 By shawl.qiu

说明:
Javascript 的 Cookie 真是有够弱的, 让你写 Cookie 值, 比如:

    linenum

  1. 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 函数

    linenum

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

2. Javascript 读 Cookie 函数

    linenum

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


抱歉!评论已关闭.