티스토리 뷰

program

jquery.cookie 에 object 넣기

littlecarbb 2019. 3. 29. 17:28

query.cookie.js 다운 : https://github.com/carhartl/jquery-cookie


cookie.js 삽입
(function ($) {
    $.fn.extend({
        cookieList: function (cookieName) {
        
            return {
                add: function (val) {
                    var array    = this.items();

                    //현재  동일값 존재 여부 확인 후 없으면 넣는다.
                    console.log("this.items().indexOf(val):"+this.indexOf(val));
                    console.log("this.items().length():"+this.length());
                    //this.hasitem(val);
                    console.log(typeof(this.hasitem(val)));
                    if(typeof(this.hasitem(val))== "undefined"){
                        array.push(val);
                        
                        var inStr = JSON.stringify(array);
                        $.cookie(cookieName, inStr, { expires:360, path: '/' });
                        
                        
                    }else{
                        
                        console.log(this.items().indexOf(val));
                        console.log("index 존재:"+val);
                    }
            
                },
                remove: function (val) {
                    var items = this.items();
                    
                    var index = items.indexOf(val);
                    
                    if (index != -1) {
                        items.splice(index, 1);
                        $.cookie(cookieName, items.join(','), { expires: 360, path: '/' });
                    }
                },
                indexOf: function (val) {
                    return this.items().indexOf(val);
                },
                clear: function () {
                    $.cookie(cookieName, null, { expires: 360, path: '/' });
                },
                items: function () {
                    var cookie = $.cookie(cookieName);
                    return cookie ? JSON.parse(cookie):[];
                },
                hasitem: function (val){
                    for (var k in this.items()){
                        var entry1 = JSON.stringify(this.items()[k]);
                        var val1 = JSON.stringify(val);
                        if(val1 == entry1){
                            console.log("key:"+k);
                            return k;
                        }
                    }
                },
                length: function () {
                    return this.items().length;
                },
            };
        }
    });
})(jQuery);


실제 사용 HTML

<!DOCTYPE html>
<html lang="kr">
<head>

<meta charset="utf-8">

<!-- Le styles -->


<script src="jquery.cookie.js"></script>
<script src="cookie.js"></script>

<script>
    
  var cookieList = $.fn.cookieList("userCookie9");
  function setCookie(url, name, age){
      
     var obj = new Object();
      obj.url = url;
      obj.name= name;
      obj.age = age;

      cookieList.add(obj);
  }
  
  
  function getResult(){
      for (var k in cookieList.items()){
        //console.log("url:"+cookieList.items()[k].url);
        //console.log("name:"+cookieList.items()[k].name);
        if(cookieList.items()[k].url) {
            console.log("url:"+cookieList.items()[k].url);
            $('#msg').append(cookieList.items()[k].url);
        }
        //console.log("age:"+itemList[k].age);
    }
  }
  
    function getReverse(){
        var itemList = cookieList.items();
        itemList.reverse();
      for (var k in itemList){
        console.log("url:"+itemList[k].url);
        console.log("name:"+itemList[k].name);
        console.log("age:"+itemList[k].age);
    }
                    
  }
  

$(function(){
    setCookie("나의 url 2", "나의 이름 2", "18");
    getResult();//입력순서대로 가져오기
});
  

 // getReverse();//최신 입력순으로 가져오기

  
  
  
</script>
</head>
<body>
<div id="msg"></div>
    
</body>
</html>

공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
글 보관함