获取cookie 设置cookie

本文档详细介绍了如何在前端设置和获取cookies,包括setCookie函数实现 cookie 存储30天,并getCookie函数用于检索特定名称的cookie。适合理解浏览器存储机制的开发者使用。
摘要由CSDN通过智能技术生成
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;
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值