使用javascript在Cookie中创建数组 [英] Create array in cookie with javascript

查看:159
本文介绍了使用javascript在Cookie中创建数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以使用数组创建cookie?



我想存储 a [0] ='peter' a ['1'] ='esther' a ['2'] ='john'

正如你可以在这个主题



您结合使用 jQuery.cookie 插件和 JSON 并解决您的问题。



当您要存储数组时,可以在JS中创建一个数组,并使用 JSON.stringify $。cookie('name','array_string')

  var myAry = [1,2,3]; 
$ .cookie('name',JSON.stringify(myAry));

当你想要在cookie中找回数组时,使用 $。 cookie('name')来检索cookie值,并使用 JSON.parse 从字符串中取出数组。

  var storedAry = JSON.parse($。cookie('name')); 
// storedAry - > [1,2,3]


Is it possible to create a cookie using arrays?

I would like to store a[0]='peter', a['1']='esther', a['2']='john' in a cookie in JavaScript.

解决方案

As you can read in this topic:

You combine the use jQuery.cookie plugin and JSON and solve your problem.

When you want to store an array, you create an array in JS and use JSON.stringify to transform it into an string and stored with $.cookie('name', 'array_string')

var myAry = [1, 2, 3];
$.cookie('name', JSON.stringify(myAry));

When you want to retrive the array inside the cookie, you use $.cookie('name') to retrive the cookie value and use JSON.parse to retrive the array from the string.

var storedAry = JSON.parse($.cookie('name'));
//storedAry -> [1, 2, 3]

这篇关于使用javascript在Cookie中创建数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆