开发者

Looking for a good way to write integers to a cookie

What is a good client-side code construct to store 5 integers that can be added to or removed easily?

I have a number of checkboxes with unique IDs. When clicked, the ID is stored in a cookie so they're available next time the user visits the site.

Is it better for me to have a JavaScript object store the values 开发者_C百科and write that to a cookie, or write the data directly to the cookie?

Are there any good code samples?


If your data is limited to integers, you can easily store them in one cookie using a delimiter.

E.g.

var theData = [46, 45, 87, 2015, 6];
document.cookie = "mydata=" + theData.join('|');

will store a cookie named mydata as 46|45|87|2015|6. Of course you can simply push data to the array, etc.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜