Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to store an array in jquery cookie?

Tags:

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

like image 806
mukamaivan Avatar asked Dec 24 '09 19:12

mukamaivan


People also ask

How to save array in cookie jQuery?

So on any page you can get the items like this. var list = new cookieList("MyItems"); // all items in the array. list. add("foo"); //Note this value cannot have a comma "," as this will spilt into //two seperate values when you declare the cookieList.

Can I store array in cookie?

Cookies can only store string values. You cannot store an array directly into a cookie.

Can we store object in cookie?

Store objects in the CookiesIf users want to store any other types of data in the cookies, they need to convert it to the string using the stringify() method. In this section, we will convert the object to a string and store it in cookies. Also, we will retrieve the object from the cookies.


1 Answers

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.         //EDIT: Modified from linked answer by Nick see          //      http://stackoverflow.com/questions/3387251/how-to-store-array-in-jquery-cookie         $.cookie(cookieName, items.join(','));     },     "remove": function (val) {          //EDIT: Thx to Assef and luke for remove.         indx = items.indexOf(val);          if(indx!=-1) items.splice(indx, 1);          $.cookie(cookieName, items.join(','));        },     "clear": function() {         items = null;         //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.

EDIT See bravos answer if you having issues with IE

like image 91
almog.ori Avatar answered Sep 19 '22 00:09

almog.ori