zoukankan      html  css  js  c++  java
  • vue设置cookie和获取cookie

    写法1: 

    methods: {
      //设置cookie
          setCookie: function (cname, cvalue, exdays) {
            var d = new Date();
            d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
            var expires = "expires=" + d.toUTCString();
            console.info(cname + "=" + cvalue + "; " + expires);
            document.cookie = cname + "=" + cvalue + "; " + expires;
            console.info(document.cookie);
          },
          //获取cookie
          getCookie: function (cname) {
            var name = cname + "=";
            var ca = document.cookie.split(';');
            console.log("获取cookie,现在循环")
            for (var i = 0; i < ca.length; i++) {
              var c = ca[i];
              console.log(c)
              while (c.charAt(0) == ' ') c = c.substring(1);
              if (c.indexOf(name) != -1){
                return c.substring(name.length, c.length);
              }
            }
            return "";
          },
          //清除cookie
          clearCookie: function () {
            this.setCookie("username", "", -1);
          },
          checkCookie: function () {
            var user = this.getCookie("username");
            if (user != "") {
              alert("Welcome again " + user);
            } else {
              user = prompt("Please enter your name:", "");
              if (user != "" && user != null) {
                this.setCookie("username", user, 365);
              }
            }
          }
        },
        created() {
          scroll(0, 0);
          this.checkCookie();
        }

    写法2:

        //设置cookie

        /**

        * 设置cookie

        * name cookie的名称

        * value cookie的值

        * day cookie的过期时间

        */

        Vue.prototype.setCookie = function (name, value, day) {

            if (day !== 0) { //当设置的时间等于0时,不设置expires属性,cookie在浏览器关闭后删除

                var curDate = new Date();

                var curTamp = curDate.getTime();

                var curWeeHours = new Date(curDate.toLocaleDateString()).getTime() - 1;

                var passedTamp = curTamp - curWeeHours;

                var leftTamp = 24 * 60 * 60 * 1000 - passedTamp;

                var leftTime = new Date();

                leftTime.setTime(leftTamp + curTamp);

                document.cookie = name + "=" + escape(value) + ";expires=" + leftTime.toGMTString();

            } else {

                document.cookie = name + "=" + escape(value);

            }

        }

        Vue.prototype.getCookie = function (name) {

        var arr;

        var reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)");

        if (arr = document.cookie.match(reg))

            return unescape(arr[2]);

        else

            return null; ocument.cookie = name + "=" + escape(value);

        }

        //在页面中获取cookie  let showcookie = this.getCookie('wxshowstated');

        //在页面中设置cookie  this.setCookie('wxshowstated', '1', 360);

  • 相关阅读:
    JS--XML 操作
    JS--中的 Cookie 与存储
    php qr生成二维码
    jQuery cookie插件保存用户登陆信息
    linux BASH shell设置字体与背景颜色
    linux iostat命令详解 磁盘操作监控工具
    Nginx下10个安全问题提示
    PHP计算某个目录大小的方法
    php验证是否是md5编码的代码
    JS正则表达式获取字符串中特定字符
  • 原文地址:https://www.cnblogs.com/yugueilou/p/15073994.html
Copyright © 2011-2022 走看看