您当前的位置:主页 > 教程合集 > Html/css > 网站首页Html/css
Javascript的cookie相关函数
发布时间:2022-03-21 23:37:00编辑:余斗阅读:(0)字号: 大 中 小
直接上代码:
// 新建cookie。
function setCookie(name, value, hours, path) {
var name = encodeURIComponent(name);
var value = encodeURIComponent(value);
var expires = new Date();
expires.setTime(expires.getTime() + hours * 3600000);
path = path == "" ? "" : ";path=" + path;
_expires = (typeof hours) == "string" ? "" : ";expires="
+ expires.toUTCString();
document.cookie = name + "=" + value + _expires + path;
}
// 获取cookie值
function getCookieValue(name) {
var name = encodeURIComponent(name);
var allcookies = document.cookie;
name += "=";
var pos = allcookies.indexOf(name);
if (pos != -1) {
var start = pos + name.length;
var end = allcookies.indexOf(";", start);
if (end == -1)
end = allcookies.length;
var value = allcookies.substring(start, end);
return decodeURIComponent(value);
} else
return "";
}
// 删除cookie
function deleteCookie(name, path) {
var name = encodeURIComponent(name);
var expires = new Date(0);
path = path == "" ? "" : ";path=" + path;
document.cookie = name + "=" + ";expires=" + expires.toUTCString() + path;
}
关键字词:JavaScriptCookie
下一篇:JS判断页面是否加载完成的方法