• js实现cookie读写的简单操作


    @{
        ViewBag.Title = "Index1";
    }
    <h2>Index1</h2>
    <input id="Button1" type="button" value="加入" onclick="save()" />
    <input id="Button1" type="button" value="显示" onclick="load()" />
    <div id="div"></div>
    <script>
        
        if (getCookie("AAA") == null) {
            setCookie("AAA", "[]");
        }
        //添加  保存数据
        function save() {
            //获取数据
            var x = { Name: '电视', Price: 1800, desc: 'dkjajglkd', count: 1 };
            //从AAA中获取字符串
            var liststr = getCookie('AAA');
            //将字符串强转为json格式
            var list = JSON.parse(liststr);
            //将数据添加list中
            list.push(x);
            //将json格式的list强转为字符串 并赋值到AAA中
            var t = setCookie("AAA", JSON.stringify(list));
            location.href = '/Home/Index2';
        }
        //显示
        function load() {
            //从AAA中获取数据
            var liststr = getCookie('AAA');
            var list = JSON.parse(liststr);
            var x = "";
            $(list).each(function () {
                x += this.Name + ":" + this.Price + ":" + this.desc + "<br>";
            });
            //存入div显示
            $("#div").html(x);
        }
       
        /**
    * cookie中存值
    * */
        function setCookie(name, value) {
            if (value) {
                var days = 1; //定义一天
                var exp = new Date();
                exp.setTime(exp.getTime() + days * 24 * 60 * 60 * 1000);
                // 写入Cookie, toGMTString将时间转换成字符串
                document.cookie = name + "=" + escape(value) + ";expires=" + exp.toGMTString;
            }
        };

        /**
    * cookie中取值
    * */
        function getCookie(name) {
            var arr, reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)"); //匹配字段
            if (arr = document.cookie.match(reg)) {
                return unescape(arr[2]);
            } else {
                return null;
            }
        };
     
    </script>
  • 相关阅读:
    HDU 2883 kebab
    CSUOJ 1635 Restaurant Ratings
    CSUOJ 1638 Continued Fraction
    POJ 1852 Ants
    ZOJ 3471 Most Powerful
    CSUOJ 1637 Yet Satisfiability Again!
    如何生成CA证书
    Keepalived实现双机热备
    Nginx负载均衡的优缺点
    负载均衡之 nginx
  • 原文地址:https://www.cnblogs.com/sxkang/p/13215918.html
Copyright © 2020-2023  润新知