zl程序教程

您现在的位置是:首页 >  前端

当前栏目

可兼容IE的获取及设置cookie的jquery.cookie函数方法

jQuery方法Cookie 函数 设置 获取 兼容 IE
2023-06-13 09:15:04 时间
前言

在开发过程中,因为之前有接触过Discuz,就直接拿其common.js里面的getcookie和setcookie方法来使用,做到后面在使用IE来测试的时候,发现这两个方法子啊IE下不起作用,就请教同事,这样就有了jquery.cookie.js文件的由来,里面的代码很少,我贴在下面,方便以后使用和研究吧。

源码
复制代码代码如下:

/**
*Cookieplugin
*
*Copyright(c)2006KlausHartl(stilbuero.de)
*DuallicensedundertheMITandGPLlicenses:
*http://www.opensource.org/licenses/mit-license.php
*http://www.gnu.org/licenses/gpl.html
*
*/

/**
*Createacookiewiththegivennameandvalueandotheroptionalparameters.
*
*@example$.cookie("the_cookie","the_value");
*@descSetthevalueofacookie.
*@example$.cookie("the_cookie","the_value",{expires:7,path:"/",domain:"jquery.com",secure:true});
*@descCreateacookiewithallavailableoptions.
*@example$.cookie("the_cookie","the_value");
*@descCreateasessioncookie.
*@example$.cookie("the_cookie",null);
*@descDeleteacookiebypassingnullasvalue.
*
*@paramStringnameThenameofthecookie.
*@paramStringvalueThevalueofthecookie.
*@paramObjectoptionsAnobjectliteralcontainingkey/valuepairstoprovideoptionalcookieattributes.
*@optionNumber|DateexpiresEitheranintegerspecifyingtheexpirationdatefromnowonindaysoraDateobject.
*Ifanegativevalueisspecified(e.g.adateinthepast),thecookiewillbedeleted.
*Ifsettonulloromitted,thecookiewillbeasessioncookieandwillnotberetained
*whenthethebrowserexits.
*@optionStringpathThevalueofthepathatributeofthecookie(default:pathofpagethatcreatedthecookie).
*@optionStringdomainThevalueofthedomainattributeofthecookie(default:domainofpagethatcreatedthecookie).
*@optionBooleansecureIftrue,thesecureattributeofthecookiewillbesetandthecookietransmissionwill
*requireasecureprotocol(likeHTTPS).
*@typeundefined
*
*@name$.cookie
*@catPlugins/Cookie
*@authorKlausHartl/klaus.hartl@stilbuero.de
*/

/**
*Getthevalueofacookiewiththegivenname.
*
*@example$.cookie("the_cookie");
*@descGetthevalueofacookie.
*
*@paramStringnameThenameofthecookie.
*@returnThevalueofthecookie.
*@typeString
*
*@name$.cookie
*@catPlugins/Cookie
*@authorKlausHartl/klaus.hartl@stilbuero.de
*/
jQuery.cookie=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;
}
};