最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to store an array in a JavaScript cookie? - Stack Overflow

programmeradmin4浏览0评论

Does anyone have a piece of JavaScript code that creates a cookie and stores an array in it? If you also have the code to read through through cookie and delete it, that would be great as well. Thanks!

Does anyone have a piece of JavaScript code that creates a cookie and stores an array in it? If you also have the code to read through through cookie and delete it, that would be great as well. Thanks!

Share Improve this question edited Mar 22, 2014 at 15:01 fzzle 1,4945 gold badges24 silver badges29 bronze badges asked Aug 4, 2010 at 18:14 JoshJosh 3,61114 gold badges51 silver badges72 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 11

have a look at: http://plugins.jquery.com/project/cookie https://plugins.jquery.com/cookie/

to store an array

$.cookie('COOKIE_NAME', escape(myarray.join(',')), {expires:1234});

to get it back

cookie=unescape($.cookie('COOKIE_NAME'))
myarray=cookie.split(',')

jQuery, Cookie plugin:
Converting an array into a string:

> JSON.stringify([1, 2]);
> '[1, 2]'

Then:

$.cookie('cookie', '[1, 2]');

And then parse it:

JSON.parse($.cookie('cookie'));
> [1, 2]
发布评论

评论列表(0)

  1. 暂无评论