I am creating an array in my code which looks like this
Array [ "type", "year", "week" ]
When I save this to a cookie and read it again it formats as
Array [ "type,year,week" ]
How can I keep the original format Array [ "type", "year", "week" ]
I guess it is getting stripped down to a CSV when it is added to the cookie.
Thanks in advance
My code:
var myArray = [ "type", "year", "week" ]
$.cookie('setup', myArray, { path: '/' }); // set up cookie
Cookies store string values.
You need to serialize your array (with JSON or join with a predefined delimiter) before storing it into a cookie and deserialize it when reading it back.
For example:
// store into cookie
$.cookie('setup', myArray.join('|'), { path: '/' });
OR
$.cookie('setup', JSON.stringify(myArray), { path: '/' });
// read from cookie
myArray = $.cookie('setup').split('|');
OR
myArray = JSON.parse($.cookie('setup'));
Note : The JSON versions are safer since they'll work with any kind of array. The former assumes that your array elements do not contain |
in them.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.