Is it possible, I have a some sort of list and I want to store it on browser, if it is not possible, what is the efficient way of doing this?
Cookies are basically text, so you can store an array by encoding it as a JSON string (see json_encode ).
In Javascript, an array is a Hashtable Object type so the interpreter doesn't need to keep track of physical memory and changing the value of an element doesn't affect other elements as they're not stored in a contiguous block of memory.
An array is an ordered collection of values: each value is called an element, and each element has a numeric position in the array, known as its index. JavaScript lets us create arrays inside array called Nested Arrays.
If 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.
JSON encode it, effectively producing a string like "{name:'myname',age:'myage'}"
which you put in a cookie, retrieve when needed and decode back into a JavaScript array/object.
Example - store array in a cookie:
var arr = ['foo', 'bar', 'baz']; var json_str = JSON.stringify(arr); createCookie('mycookie', json_str);
Later on, to retrieve the cookie's contents as an array:
var json_str = getCookie('mycookie'); var arr = JSON.parse(json_str);
Note: cookie functions are not native, taken from How do I create and read a value from cookie?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With