处理cookie的三个f辅助函数:writeCookie, readCookie, clearCookie

本文介绍了JavaScript中处理cookie的三个关键函数:writeCookie用于设置cookie,readCookie用于读取cookie,clearCookie用于删除cookie。通过示例代码详细解释了每个函数的工作原理和用法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

function writeCookie(name, value, domain, days )
{ 
var expire = ""; //by default, there is no expiration so the cookie is temporary
var domain = "; domain=" + domain + "; path=/"; 
//specifying a number of days makes the cookie persistent
if(days) 
{ 
expire = new Date((new Date()).getTime() + days * 24 * 60 * 60 * 1000); 
expire = "; expires=" + expire.toGMTString(); 
} 
//set the cookie to the name, value, and expiration date
document.cookie = name + "=" + escape(value) + expire + domain; 
}


function readCookie(name){
//find the specified cookie and return its value
var searchName = name + "=";
var cookie = document.cookie.split(';');
for(var i = 0 ; i < cookie.length; i++){
var c = cookie[i];
while(c.charAt(0) == ' ')
c = c.substring(1, c.length);
if(c.indexOf(searchName) == 0)
return c.substring(searchName.length, c.length);
}
return null;
}


function eraseCookie(name){
//erase the specified cookie
writeCookie(name,"",""-1);
}
-----------------------------------------------------------------
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值