js : json和 cookie 的简单操作

使用 cookie,可以记录用户的最近的浏览历史 

<!DOCTYPE HTML>
<html lang="zh-cn">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
    <script src="./jquery.min.js"></script>
    <script src="./jquery.cookie.js"></script>
    <script>
        $(function(){
            $("#cookie").css("color", "red");

            var details = $.cookie("details");
            var lenth = 0;
            if(details){
                details = JSON.parse(details);
                lenth = details.length;
                $.each(details, function(index, obj){
                    $("#cookie").append("<p>name : " + obj.name + ", " + "age : " + obj.age + "</p>");
                });
            }else{
                var data = [{"name" : "keen", "age" : 24},
                            {"name" : "andi", "age" : 18}];
                $.cookie("details", JSON.stringify(data));
            }
        });
    </script>
</head>
<body>

    <div id="cookie">cookie</div>

</body>
</html>

 

========>>>>

posted @ 2017-01-05 21:01  KeenLeung  阅读(720)  评论(0编辑  收藏  举报