export const setCookie = (name, value) => {
const Days = 30; // 此 cookie 将被保存 30 天
const exp = new Date(); // new Date("December 31, 9998");
exp.setTime(exp.getTime() + Days * 24 * 60 * 60 * 1000);
document.cookie = `${name}=${escape(value)};expires=${exp.toGMTString()};path=/`; // + Config.content;
};
export const getCookie = name => {
const search = `${name}=`; // 查询检索的值
let returnvalue = ''; // 返回值
if (document.cookie.length > 0) {
let sd = document.cookie.indexOf(search);
if (sd != -1) {
sd += search.length;
let end = document.cookie.indexOf(';', sd);
if (end == -1) end = document.cookie.length;
returnvalue = unescape(document.cookie.substring(sd, end));
}
}
return returnvalue;
};
获取cookie 设置cookie
最新推荐文章于 2024-09-22 11:17:00 发布
本文档详细介绍了如何在前端设置和获取cookies,包括setCookie函数实现 cookie 存储30天,并getCookie函数用于检索特定名称的cookie。适合理解浏览器存储机制的开发者使用。
摘要由CSDN通过智能技术生成