美文网首页
cookie-util.js

cookie-util.js

作者: 泠泉 | 来源:发表于2019-04-24 09:52 被阅读0次

    //更好的方式:import Cookies from 'js-cookie'

    export function delCookie (name) {
      let exp = new Date()
      exp.setTime(exp.getTime() - 1)
      var cval = getCookie(name)
      if (cval != null) {
        document.cookie = name + '=' + cval + ';expires=' + exp.toGMTString()
      }
    }
    
    export function setCookie (name, value, time) {
      var exp = new Date()
      exp.setTime(exp.getTime() + time * 1)
      document.cookie = name + '=' + escape(value) + ';expires=' + exp.toGMTString()
    }
    
    export function getCookie (name) {
      let arr = document.cookie
      let reg = new RegExp('(^| )' + name + '=([^;]*)(;|$)')
      if (arr.match(reg)) {
        return unescape(arr[2])
      } else {
        return null
      }
    }
    
    

    相关文章

      网友评论

          本文标题:cookie-util.js

          本文链接:https://www.haomeiwen.com/subject/pydwgqtx.html