Jquery 操作Cookie

$.extend({

/**

1.设置cookie的值,把name变量的值设为value

example$.cookie(’name’,‘value’);

2.新建一个cookie包括有效期路径域名等

example$.cookie(’name’,‘value’,{expires:7,path:‘/’,domain:‘jquery.com’,secure:true});

3.新建cookie

example$.cookie(’name’,‘value’);

4.删除一个cookie

example$.cookie(’name’,null);

5.取一个cookie(name)值给myvar

varaccount=$.cookie('name');

**/

cookieHelper:function(name,value,options){

if(typeofvalue!='undefined'){//nameandvaluegiven,setcookie

options=options||{};

if(value===null){

value='';

options.expires=-1;

}

varexpires='';

if(options.expires&&(typeofoptions.expires=='number'||options.expires.toUTCString)){

vardate;

if(typeofoptions.expires=='number'){

date=newDate();

date.setTime(date.getTime()+(options.expires*24*60*60*1000));

}else{

date=options.expires;

}

expires=';expires='+date.toUTCString();//useexpiresattribute,max-ageisnotsupportedbyIE

}

varpath=options.path?';path='+options.path:'';

vardomain=options.domain?';domain='+options.domain:'';

varsecure=options.secure?';secure':'';

document.cookie=[name,'=',encodeURIComponent(value),expires,path,domain,secure].join('');

}else{//onlynamegiven,getcookie

varcookieValue=null;

if(document.cookie&&document.cookie!=''){

varcookies=document.cookie.split(';');

for(vari=0;i<cookies.length;i++){

varcookie=jQuery.trim(cookies[i]);

//Doesthiscookiestringbeginwiththenamewewant?

if(cookie.substring(0,name.length+1)==(name+'=')){

cookieValue=decodeURIComponent(cookie.substring(name.length+1));

break;

}

}

}

returncookieValue;

}

}

});

相关推荐