欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

获取cookie 设置cookie

程序员文章站 2022-05-24 14:35:45
...
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;
};
相关标签: javascript