Create array in cookie with javascript

后端 未结 9 872
-上瘾入骨i
-上瘾入骨i 2020-12-08 10:32

Is it possible to create a cookie using arrays?

I would like to store a[0]=\'peter\', a[\'1\']=\'esther\', a[\'2\']=\'john\' i

相关标签:
9条回答
  • 2020-12-08 11:07

    Create array in cookie using jQUery?

    var list = new cookieList("test"); $(img).one('click', function(i){ while($('.selected').length < 3) {
        $(this).parent()
            .addClass("selected")
            .append(setup.config.overlay);
    
        //$.cookie(setup.config.COOKIE_NAME, d, setup.config.OPTS);
        var index = $(this).parent().index();
    
        // suppose this array go into cookies.. but failed
        list.add( index );
    
        var count = 'You have selected : <span>' + $('.selected').length + '</span> deals';
        if( $('.total').length ){
            $('.total').html(count);
        }
    
    }  });
    
    0 讨论(0)
  • 2020-12-08 11:07

    You can save a lot of data within a single cookie and return it as an array, try this

      function setCookie(cookieKey, cookieValue) {
            var cookie = document.cookie.replace(/(?:(?:^|.*;\s*)idpurchase\s*\=\s*([^;]*).*$)|^.*$/, "$1")
            var idPurchase = cookie.split(",")
            idPurchase.push(cookieValue)
            document.cookie = cookieKey+"=" + idPurchase
            console.log("Id das compras efetuadas :",idPurchase)
            $("#purchases_ids option:last").after($('<option onclick="document.getElementById("input_class1").value='+cookieValue+'" value="'+cookieValue+'">'+cookieValue+'</option>'))
        }
        checkCookie()
        function checkCookie() {
            var cookie = document.cookie.replace(/(?:(?:^|.*;\s*)idpurchase\s*\=\s*([^;]*).*$)|^.*$/, "$1")
            var idPurchase = cookie.split(",")
            console.log("Id das compras efetuadas :",idPurchase)
            for (i = 1; i < idPurchase.length; i++) {
            $("#purchases_ids option:last").after($('<option value="'+idPurchase[i]+'">'+idPurchase[i]+'</option>'))
            }
            purchases_ids.addEventListener("change",changeSpeed)
        }
    

    in this project that I created I store values ​​in arrays, codePen is not allowing the storage of cookies, but in the example below you can get a complete picture, just study the implementation

    Github project

    Open in Gitpod

    0 讨论(0)
  • 2020-12-08 11:09

    As you can read in this topic:

    You combine the use jQuery.cookie plugin and JSON and solve your problem.

    When you want to store an array, you create an array in JS and use JSON.stringify to transform it into an string and stored with $.cookie('name', 'array_string')

    var myAry = [1, 2, 3];
    $.cookie('name', JSON.stringify(myAry));
    

    When you want to retrive the array inside the cookie, you use $.cookie('name') to retrive the cookie value and use JSON.parse to retrive the array from the string.

    var storedAry = JSON.parse($.cookie('name'));
    //storedAry -> [1, 2, 3]
    
    0 讨论(0)
提交回复
热议问题