关于html:取出cookie中的值得逗号等字符转换

119次阅读

共计 585 个字符,预计需要花费 2 分钟才能阅读完成。

设置 cookie:
获取 cookie:

setCookie(name, value) {console.log(name, value);
    const Days = 3000;
    const exp = new Date();
    exp.setTime(exp.getTime() + Days * 24 * 60 * 60 * 1000);
    document.cookie = name + '=' + escape(value) + ';expires=' + exp.toUTCString();}

  getCookie(name) {console.log(name);
    const strCookie = document.cookie;
    const arrCookie = strCookie.split(';');
    // tslint:disable-next-line:prefer-for-of
    for (let i = 0; i < arrCookie.length; i++) {const arr = arrCookie[i].split('=');
      console.log(arr);
      if (arr[0].trim() === name) {console.log(arr[1]);

        return arr[1];
      }
    }
    return '';
  }

cookie 存储不反对逗号等
获取 cookie 解决
const arr = str.split(‘%2C’); // %2C 对应的是 ASCII 字符“,”(英文逗号)

正文完
 0