-
Javascript 读写 Cookie 操作 By shawl.qiu
说明:
Javascript 的 Cookie 真是有够弱的, 让你写 Cookie 值, 比如:- linenum
- 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/btbtd1. Javascript 写 Cookie 函数
- linenum
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
- <script type="text/javascript">
- //<![CDATA[
- var timer=new Date();
- timer.setSeconds(timer.getSeconds()+5000);
- fWriteCookie(document.cookie, /* 读取原有 Cookie 值进行判断 */
- true, /* 是否覆盖原有的 Cookie 值 */
- /* 赋植给 Cookie, 请注意正确设置分隔符 */
- 'title=test this::subtitle=test this too::novalue::hasvalue=ok',
- true, /* 是否对 Cookie值 进行编码 */
- '##', /* Cookie 内容识别符 */
- '::', /* Cookie 内容分隔符 */
- timer.toGMTString(), /* 设置 Cookie 有效时间, 时间必须为 dateObj.toGMTString() 格式. */
- '/', /* 设置 Cookie 作用路径 */
- '', /* 设置 Cookie 作用 域名, 注意: 不能跨域, 子域除外. */
- false /* 是否使用 SSI 协议 */
- );
- /*-----------------------------------------------------------*/
- * Javascript 写 Cookie 函数 By shawl.qiu
- *-----------------------------
- * 参数说明:
- * cookie: 读取 Cookie 值
- * overwrite: 是否覆盖原有 Cookie
- * value: 作为 Cookie 值 写入 Cookie
- * encode: 是否对 Cookie 值进行编码
- * identifier: Cookie 值 识别符
- * separate: Cookie 值 分隔符
- * expires: Cookie 有效时限
- * path: Cookie 起作用路径
- * domain: Cookie 起作用域, 不能跨域, 子域除外
- * secure: 是否使用 SSI 协议
- /*-----------------------------------------------------------*/
- //----------------Begin function fWriteCookie-----------------//
- function fWriteCookie(cookie, overwrite, value, encode, identifier, separate, expires, path, domain, secure){
- if(cookie.length>0&&!overwrite)return false;
- if(!value)return false;
- var temp='';
- var array=value.split(separate);
- for(var i=0; i<array.length; i++){ // 删除没有 = 号的值
- if(array[i].indexOf('=')==-1)array.splice(i,1);
- }
- if(encode){ // 对 cookie 的值进行编码
- for(var i=0; i<array.length; i++){
- temp=array[i].split('=');
- array[i]=temp[0]+'='+escape(temp[1]);
- }
- }
- cookie=identifier+(array.join(separate))+identifier
- if(expires!=undefined) cookie+=';expires='+expires;
- if(path!=undefined) cookie+=';path='+path;
- if(domain!=undefined) if(domain!=''){cookie+=';domain='+domain;};
- if(secure!=undefined) if(secure){cookie+=';secure='+secure;};
- cookie+=';'
- document.cookie=cookie;
- } // shawl.qiu script
- //----------------End function fWriteCookie-------------------//
- //]]>
- </script>
2. Javascript 读 Cookie 函数
- linenum
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
- <script type="text/javascript">
- //<![CDATA[
- var title=subtitle=hasvalue='';
- fReadCookie(document.cookie, '##', '::', true);
- document.write('<p/>');
- document.write('title: ',title,'<br/>');
- document.write('subtitle: ',subtitle,'<br/>');
- document.write('hasvalue: ',hasvalue,'<br/>');
- /*-----------------------------------------------------------*/
- * Javascript 读取 Cookie 内容, 并动态赋值 函数 By shawl.qiu
- *-----------------------------
- * 参数说明:
- * cookie: 读取 Cookie 值
- * identifier: Cookie 值 识别符
- * separate: Cookie 值 分隔符
- * decode: 是否对 Cookie 值进行解码
- /*-----------------------------------------------------------*/
- //----------------Begin function fReadCookie-----------------//
- function fReadCookie(cookie, identifier, separate, decode){
- if(cookie.length==0)return false;
- cookie=cookie.substring(cookie.indexOf(identifier)+2, cookie.lastIndexOf(identifier));
- var array=cookie.split(separate);
- var temp='';
- for(var i=0; i<array.length; i++){ // 跳过没有 = 号的值
- if(array[i].indexOf('=')==-1)continue;
- temp=array[i].split('=');
- if(decode!=undefined){if(decode){temp[1]=unescape(temp[1]);}}
- temp[0]=temp[0].replace(/^[/s]+|[/s]+$/,'');
- temp[1]=temp[1].replace(/^[/s]+|[/s]+$/,'');
- eval(temp[0]+"=temp[1]");
- }
- } // shawl.qiu script
- //----------------End function fReadCookie-------------------//
- //]]>
- </script>