“Javascript para desarrolladores web” liburutik kopiatutako kode honekin oso erraz moldatu ahalko gara cookiekin.
Cookiak sortzeko:
function setCookie(sName, sValue, oExpires, sPath, sDomain, bSecure){
var sCookie = sName +"="+ encodeURIComponent(sValue);
if(oExpires){
sCookie += "; expires=" + oExpires.toGMTString();
}
if(sPath){
sCookie += "; path=" + sPath;
}
if(sDomain){
sCookie += "; domain=" + sDomain;
}
if(bSecure){
sCookie += "; secure";
}
document.cookie = sCookie;
}
var sCookie = sName +"="+ encodeURIComponent(sValue);
if(oExpires){
sCookie += "; expires=" + oExpires.toGMTString();
}
if(sPath){
sCookie += "; path=" + sPath;
}
if(sDomain){
sCookie += "; domain=" + sDomain;
}
if(bSecure){
sCookie += "; secure";
}
document.cookie = sCookie;
}
Cookia berreskuratzeko:
function getCookie(sName){
var sRE = "(?:; )" + sName + "=([^;]*);?";
var oRE = new RegExp(sRE);
if(oRE.test(document.cookie)){
return decodeURIComponent(RegExp["$1"]);
}else{
return null;
}
}
var sRE = "(?:; )" + sName + "=([^;]*);?";
var oRE = new RegExp(sRE);
if(oRE.test(document.cookie)){
return decodeURIComponent(RegExp["$1"]);
}else{
return null;
}
}
Cookia ezabatzeko:
function delCookie(sName, sPath, sDomain){
setCookie(sName, "", new Date(0), sPath, sDomain);
}
setCookie(sName, "", new Date(0), sPath, sDomain);
}
Cookia sortzeko funtzioaren ezabatze data zehazteko hurrengo kode zatiaz balia zaitezke, horrela modu erresean, zenbat egun barru ezabatuko den zehaztu ahalko duzu.
var zenbatEgun = 15;
var exp = new Date();
exp.setTime(exp.getTime() + (zenbatEgun*24*60*60*1000));
setCookie("cookieIzena", "nire informazioa", exp);
var exp = new Date();
exp.setTime(exp.getTime() + (zenbatEgun*24*60*60*1000));
setCookie("cookieIzena", "nire informazioa", exp);
0 Responses
Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.