#34 操作 Cookie


  • 0
    administrators

    完成 cookieJar 单例,它有三个方法:

    • set(name, value, days):设置 cookie 的值,days 为多少天以后过期。
    • get(name):获取 cookie 的值。
    • remove(name):删除 cookie 的值。

    (本题来源:阿里巴巴前端笔试题)


  • 1

    const cookieJar = {
      set (name, value, days) {
        document.cookie=`${name}=${value};expires=${new Date(Date.now()+days*24*3600*1000)}`
      },
      get (name) {
        let cookie=document.cookie
        let reg=new RegExp(`${name}=([^;]+)`)
       var result=reg.exec(cookie)
       return result[1]
      },
      remove (name) {
         document.cookie=`${name}=outdate;expires=${new Date(Date.now()-36110000)}`
      }
    }
    

    贴出我的,看看大家怎么写的


  • 0

    你这个怎么运行呀,怎么报错啊


  • 0

    const cookieJar = {
      set (name, value, days) {
        let time = new Date().getTime();
        time = time + days * 24 * 3600;
        let timeUTCStr = new Date(time)
        if(days!=0){
          let cookieStr = name+'='+value+'; expires=' + timeUTCStr.toString();
          console.log(cookieStr)
          document.cookie = cookieStr;     
        }
      },
      get (name) {
        let cookie= document.cookie.split(';')
        let result;
        if(cookie){
        cookie.forEach((vaule)=>{
          let resultArr = /([a-zA-Z0-9]+)=([a-zA-Z0-9]+)/.exec(vaule);
          if( resultArr &&resultArr[1] == name){
            result = resultArr[2]
          }
        })
        return result 
        }
      },
      remove (name) {
        let cookie= document.cookie.split(';')
        let result;
        if(cookie){
        cookie.forEach((vaule)=>{
          let resultArr = /([a-zA-Z0-9]+)=([a-zA-Z0-9]+)/.exec(vaule);
          if( resultArr && resultArr[1] == name){
             document.cookie = resultArr[1]+'='+resultArr[2]+';expires='+new Date(0).toUTCString()  
          }
        })
        return result 
        }
      }
    }
    

  • 0

    1. key value 都需要URL编码吧
    2. expires格式应该是格林威治时间
    const cookieJar = {
      set (name, value, days) {
        /* TODO */
        var cookieText = encodeURIComponent(name) + '=' + encodeURIComponent(value)
        if (days) {
          var ss = new Date().getTime() + parseInt(days) * 24 * 60 * 60 * 1000
          cookieText += '; expires=' + new Date(ss).toGMTString()
        }
        document.cookie = cookieText
      },
      get (name) {
        /* TODO */
        var cookieName = encodeURIComponent(name) + '='
        var cookieStart = document.cookie.indexOf(cookieName)
        var cookieValue = null
        if (cookieStart > -1) {
          var cookieEnd = document.cookie.indexOf(';', cookieStart)
          if (cookieEnd == -1) {
            cookieEnd = document.cookie.length
          }
          cookieValue = decodeURIComponent(document.cookie.substring(cookieStart + cookieName.length, cookieEnd))
        }
        return cookieValue
      },
      remove (name) {
        /* TODO */
        cookieJar.set(name, '')
      }
    }
    
    

登录后回复
 

与 ScriptOJ 的连接断开,我们正在尝试重连,请耐心等待