cookie存多个key

本文介绍了一种使用cookie存储多个值的方法,通过将多个值编译成字符串并设置过期时间,实现单个cookie项存储多个信息的功能。同时,提供了设置、删除和获取cookie的JavaScript代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

                                     cookie存多个值

 

 

 

正题 

 cookie存单个值 并设置过期时间 是可以 name=‘xxx’;expires=‘xxxx’ 这么写的, 但是如果要存多个值,请把你需存的值 放在字符串中编译一下然后 在放到 name=‘你编译的内容’;expires=‘xxxx’;  如果你要删除cookie 那么name=‘’;expires=‘随便一个过期时间’ 

export default {
  setCookie(info, expires) {
    console.log(info);
    if (expires) {
      expires = new Date(
        new Date().getTime() + parseInt(expires) * 24 * 60 * 60 * 1000
      );
    }
    if (info) {
      let userName = info.userName;
      let password = info.password;
      let zone = info.zone;
      let content = `userName=${userName};password=${password};zone=${zone};`;
      
      console.log(escape(content))
      document.cookie = 'token='+ escape(content) +`;${
        expires ? 'expires=' + expires : ''
      }`;
      console.log(document.cookie)
    }
  },
  delCookie() {
    let expires = new Date(new Date().getTime() - 24 * 60 * 60 * 1000);
    document.cookie = `token=''; expires=${expires}`;
  },
  getCookie() {
    console.log(document.cookie);
  }
};

token=userName%3D%u674E%u56DB%3Bpassword%3D11111111%3Bzone%3D1%3B

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值