cookie

2018-09-09  本文已影响0人  wur1

js获取/设置cookie

let cookies = {};

if (document.cookie != null) {
  document.cookie.split(';').map((cookieStr) => {
    let [key, value] = cookieStr.split('=');
    cookies[key.trim()] = value;
  });
}

export default {
  get: (key) => (cookies[key] || ''),
  set: (name, value) => {
    var exp = new Date();
    exp.setTime(exp.getTime() + 30*60*1000);
    document.cookie = name + "="+ escape (value) + ";expires=" + exp.toGMTString()+";path=/"; // escape将?!等特殊字符进行编码
  }
};
上一篇下一篇

猜你喜欢

热点阅读