views:

1759

answers:

4

Hi, I need to store an array in a jQuery cookie, any one help me please?

+1  A: 

Download the jQuery cookie plugin here: http://plugins.jquery.com/project/Cookie

Setting a cookie with jQuery is as simple as this, where we are creating a cookie called "example" with a value of ["foo1", "foo2"]

$.cookie("example", ["foo1", "foo2"]);

Getting the cookie's value is also very easy with jQuery. The following would show the value of the "example" cookie in a dialog window

alert( $.cookie("example") );
almog.ori
Thanx Ori, it works. But then my actual problem is pushing values in an array cookie onclick. After every thing is done on more than one page, i can be able to append each each array element of the cookie elements to unordered list (ul) and be able to pop out any index i a client feels like.More help please.
mukamaivan
not sure what you mean, but id start with a function that is called when you click that saves the "selection" (or whatever you need it to be) this can be in a seperate js file that the different pages reference using a script tag with url.
almog.ori
Fine my js is aready an external file.I hope this sample code gives you a clue on what i want.$(function(){ /**This is what i tend to do*/ $.cookie('cookieItem', msg.txt, { expires: 1}); myCookie_arr.push($.cookie('cookieItem'));// add elements at the end of my cookie array .. $(window).load(function() { .. alert(myCookie_arr); for(var i= 0; i < myCookie_arr; i++){ $('#item-list').append(myCookie_arr[i]);//append on visiting other pages } .. if(cookieItem){ $('#item-list').append($.cookie('cookieItem')); }else{ $('#cat').hide(); } });});
mukamaivan
my varriables are:var myCookie;var initial_arr = new Array();var myCookie_arr = new Array();var cookieItem;
mukamaivan
........ /*******This quite works but can't actually achievewhat i want***********/ $('#add_item').click(function(){ initial_arr.push(msg.txt); $.cookie('cookieItem', initial_arr, { expires: 1});//Update new cookie $('#item-list').append(msg.txt);//append on click }); /**************This is what i tend to do******************/ $.cookie('cookieItem', msg.txt, { expires: 1});//Update new cookie myCookie_arr.push($.cookie('cookieItem'));// add elements at the end of my cookie array ...........
mukamaivan
+3  A: 

Still not exactly sure what you need but i hope this will help. This is a sample that will allow you to access the items on any page, its just a sample! It uses the cookieName to identify it across the pages.

//This is not production quality, its just demo code.
var cookieList = function(cookieName) {
//When the cookie is saved the items will be a comma seperated string
//So we will split the cookie by comma to get the original array
var cookie = $.cookie(cookieName);
//Load the items or a new array if null.
var items = cookie ? cookie.split(/,/) : new Array();

//Return a object that we can use to access the array.
//while hiding direct access to the declared items array
//this is called closures see http://www.jibbering.com/faq/faq_notes/closures.html
return {
    "add": function(val) {
        //Add to the items.
        items.push(val);
        //Save the items to a cookie.
        $.cookie(cookieName, items);
    },
    "clear": function() {
        //clear the cookie.
        $.cookie(cookieName, null);
    },
    "items": function() {
        //Get all the items.
        return items;
    }
  }
}

So on any page you can get the items like this.

var list = new cookieList("MyItems"); // all items in the array.

Adding items to the cookieList

list.add("foo"); 
//Note this value cannot have a comma "," as this will spilt into
//two seperate values when you declare the cookieList.

Getting all the items as an array

alert(list.items());

Clearing the items

list.clear();

You can add additional things like push and pop quite easily. Again hope this helps.

almog.ori
Thanx Ori, i have made the functions and it worked...
mukamaivan
A: 

You could serialize the arrays before storing as cookie and then deserialize when reading. ie with json?

Flobo
A: 

Hi almog.ori and all wise guy out there, I had a failure when applying your code. When I getting list of items, it return null but when I alert in onclick event, eventually it push value into that array. Can you help me with this, thank in advance.. Sample Code :

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);
    }

} 
});

When I check through Firefox cookies, it seem doesn't save cookie session.

You should better this as a separate thread, notas an answer. That way more people would see it and try to answer.You can of course link back to this question for reference.
sth