function setCookie(name, value, secs)
{
var exp = new Date();
exp.setTime(exp.getTime() + secs*1000);
arr = document.domain.split(".");
if (arr.length == 2) {
domain = "."+document.domain;
} else {
domain = "."+arr[arr.length-2]+"."+arr[arr.length-1];
}
document.cookie = name+"="+encodeURIComponent(value)+";expires="+exp.toGMTString()+";path=/;domain="+domain;
}
function getCookie(name)
{
var arr,reg=new RegExp("(^| )"+name+"=([^;]*)(;|$)");
if (arr=document.cookie.match(reg)) {
return decodeURIComponent(arr[2]);
}
return null;
}js设置页面cookie
最新推荐文章于 2024-07-26 08:08:07 发布
本文介绍了一段JavaScript代码,用于实现自定义的setCookie和getCookie函数。通过这些函数,开发者可以轻松地为网站设置Cookie并在需要时检索它们。代码详细说明了如何根据指定的时间来设置Cookie的有效期,并考虑到不同域名的适用情况。
1万+

被折叠的 条评论
为什么被折叠?



