js封装cookie

const cookie = {
  //写cookies 
  setCookie: function(name, value, day=1) { 
    let days = day
    let exp = new Date()
    exp.setTime(exp.getTime() + days*24*60*60*1000)
    document.cookie = name + '=' + escape (value) + ';expires=' + exp.toGMTString()
  },
  //读取cookies 
  getCookie: function (name) {
    let arr = null
    let reg = new RegExp('(^| )'+name+'=([^;]*)(;|$)')
    if (document.cookie && (arr = document.cookie.match(reg))) {
      return unescape(arr[2])
    } else {
      return null;
    } 
  }, 
  //删除cookies 
  delCookie: function (name) { 
    let cval = this.readCookie(name)
    if (cval!=null) {
      document.cookie = name + '=' + cval + ';expires=' + (new Date(0)).toGMTString()
    }
  }  
}

export default cookie

 

你可能感兴趣的:(javascript,前端开发,js,cookie,前端)