文章摘要
这篇文章介绍了两个与Cookie管理相关的JavaScript函数: 1. `setCookie(c_name, value, expiredays)` 函数用于设置Cookie。它接收三个参数:Cookie名称、值和过期天数。该函数会创建一个日期对象,延长过期天数,并将Cookie设置为指定格式(如 `c_name=value;expires=日期`)。 2. `getCookie(c_name)` 函数用于从文档中提取特定Cookie的值。它通过查找Cookie中的指定名称,提取值和过期日期。如果Cookie格式不正确或未找到对应名称,则返回空字符串。 文章重点围绕两个函数的实现逻辑,展示了如何通过JavaScript实现Cookie的设置与提取功能。
function setCookie(c_name,value,expiredays)
{
var exdate=new Date()
exdate.setDate(exdate.getDate()+expiredays)
document.cookie=c_name+ “=” +escape(value)+
((expiredays==null) ? “” : “;expires=”+exdate.toGMTString())
}
function setCookie(c_name,value,expiredays)
{
var exdate=new Date()
exdate.setDate(exdate.getDate()+expiredays)
document.cookie=c_name+ “=” +escape(value)+
((expiredays==null) ? “” : “;expires=”+exdate.toGMTString())
}
//取回cookie
function getCookie(c_name)
{
if (document.cookie.length>0)
{
c_start=document.cookie.indexOf(c_name + “=”)
if (c_start!=-1)
{
c_start=c_start + c_name.length+1
c_end=document.cookie.indexOf(“;”,c_start)
if (c_end==-1) c_end=document.cookie.length
return unescape(document.cookie.substring(c_start,c_end))
}
}
return “”
}
© 版权声明
文章版权归作者所有,未经允许请勿转载。