zoukankan      html  css  js  c++  java
  • 设置cookie

    下午写了一个简单的访问cookie的对象,贴出来先

    文件1: realwall.js

    /*
     * 将访问cookie的方法封装到realwall对象里
     */
    (function(window){
        var document = window.document;
        var realwall = {
            setCookie: function(name, value, expiredays){
                var cookieStr = encodeURIComponent(name) + "=" + encodeURIComponent(value),
                    exDate = new Date();
                exDate.setDate(exDate.getDate() + expiredays);
                cookieStr += "; expires=" + exDate.toUTCString() + "; path=/";
                document.cookie = cookieStr;
            },
            getCookie: function(name){
                var cookieStr = document.cookie,
                    start = cookieStr.indexOf(encodeURIComponent(name) + "="),
                    end = 0;
                if(start > -1){
                    end = cookieStr.indexOf(";", start);
                    if(end == -1){
                        end = cookieStr.length;
                    }
                    var value = decodeURIComponent(cookieStr.substring(start + encodeURIComponent(name).length + 1, end));
                    return value;
                }
            }
        };
        window.realwall = realwall;
    })(window);
    

    文件2:ts.js

    /*
     * 调用realwall对象的方法,并在cookie中放入json格式数据,并读取之
     */
    window.onload = function(){
        var value = prompt("Please input the value of name:");
        //在此输入:  {'aa':'hi'}
        realwall.setCookie("key", value, 2);
        var key= realwall.getCookie("key");
        var json = eval("(" + key+ ")");
        alert(json.aa);
    }
    

      

  • 相关阅读:
    重载运算符 && 构造函数 的写法
    2019 ICPC Asia Xuzhou Regional
    中国剩余定理
    求逆元
    Exgcd
    Leading Robots
    大家好
    AtCoder Grand Contest 047 部分题解
    CodeForces 1389E Calendar Ambiguity 题解
    CodeForces 1380F Strange Addition 题解
  • 原文地址:https://www.cnblogs.com/realwall/p/2165570.html
Copyright © 2011-2022 走看看