Javascript 讀寫 Cookie 操作 By shawl.qiu

來源:互聯網
上載者:User

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>


相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.