cookie的设置、获取、清除

博客中有很多,记录下自己看过的比较认可的。
目前还有点问题 document.cookie 为空;
失效时间是在当前时间之后的

补充解决方案

必须通过服务器的方式打开这个文件
Mac下使用服务器的方式打开html文件:

  • mac 自带 Apache 服务器,使用命令 sudo apachectl start, 然后输入密码;
  • 在浏览器中打开 127.0.0.1 或者 localhost,如果显示的是 it works,则说明启动成功’
  • 在 finder 中前往文件夹 /资源库/WebServer/Documents,在文件夹中放入你刚写的html 文件或者文件夹,然后可以按照正常的路径对文件进行访问。
  •  重启apache:sudo apachectl restart 
    
  •  关闭apache:sudo apachectl stop 
    
  •  开启apache:sudo apachectl start
    

在这里插入图片描述

   <label>cookie名:</label><input id="cookie_key" type="text" /> <br />
    <label>cookie值:</label><input id="cookie_val" type="text" /> <br />
    <label>失效时间:</label
    ><input id="cookie_time" type="text" title="时间格式(10s,2m等)" /> <br />
    <br />
    <input type="button" value="添加cookie" onClick="addCookieFun()" /> <br>
    <input type="button" value="检查cookie" onClick="chkCookieFun()" /> <br>
    <input type="button" value="删除cookie" onClick="delCookieFun()" /> <br>

   //获取时间的秒数(参数:d,h,m,s) 10m
      function getSecond(str) {
        var str1 = str.substr(0, str.length - 1); //时间数值
        var str2 = str.substr(str.length - 1, 1); //时间单位
        if (str2 == "s") {
          return str1 * 1000;
        } else if (str2 == "m") {
          return str1 * 60 * 1000;
        } else if (str2 == "h") {
          return str1 * 60 * 60 * 1000;
        } else if (str2 == "d") {
          return str1 * 24 * 60 * 60 * 1000;
        }
      }

      //添加cookie
      function addCookie(name, value, time) {
        var strSec = getSecond(time);
        var exp = new Date();

        exp.setTime(exp.getTime() + strSec * 1);

        //设置cookie的名称、值、失效时间
        document.cookie = `${name}= ${value}; expires=${exp.toLocaleTimeString()}`;
        console.log(c
      }

      //获取cookie
      function getCookie(name) {
        //获取当前所有cookie
        var strCookies = document.cookie;
        //截取变成cookie数组
        var array = strCookies.split(";");
        //循环每个cookie
        for (var i = 0; i < array.length; i++) {
          //将cookie截取成两部分
          var item = array[i].split("=");
          //判断cookie的name 是否相等
          if (item[0] == name) {
            return item[1];
          }
        }
        return null;
      }

      //删除cookie
      function delCookie(name) {
        var exp = new Date();
        exp.setTime(exp.getTime() - 1);
        //获取cookie是否存在
        var value = getCookie(name);
        if (value != null) {
          document.cookie =`${name}= ${value}; expires=${exp.toLocaleTimeString()}`
        }
      }

      function addCookieFun() {
        var cookie_key = document.getElementById("cookie_key").value;
        var cookie_val = document.getElementById("cookie_val").value;
        var cookie_time = document.getElementById("cookie_time").value;
        addCookie(cookie_key, cookie_val, cookie_time);
        console.log("添加cookie:" + cookie_key, document.cookie);
      }

      function chkCookieFun() {
        var cookie_key = document.getElementById("cookie_key").value;
        var result = getCookie(cookie_key);
        console.log(result, "res");

        if (result != null) {
          console.log("存在cookie:" + cookie_key);
        } else {
          console.log("不存在cookie:" + cookie_key);
        }
      }

      function delCookieFun() {
        var cookie_key = document.getElementById("cookie_key").value;
        delCookie(cookie_key);
        console.log("删除cookie:" + cookie_key);
      }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值