我们可以用javascript将对象分配到cookie中吗?

时间:2021-07-20 20:50:30

Does anyone know is it possible to assign an object into cookies in javascript? If yes, how can we do it?

有人知道用javascript将对象分配到cookie中有可能吗?如果是,我们怎么做?

7 个解决方案

#1


6  

If you can serialize your object into its canonical string representation, and can unserialize it back into its object form from said string representation, then yes you can put it into a cookie.

如果您可以将对象序列化为其规范的字符串表示形式,并可以从上述字符串表示形式将其反序列化为对象形式,那么您可以将其放入cookie中。

Judging from your earlier question regarding storing the result of window.open() into a cookie, this is not the answer you are hoping for.

从前面关于将window.open()的结果存储到cookie中的问题来看,这并不是您希望的答案。

#2


2  

This is one way to do it,

这是一种方法,

  1. Serialize your object into JSON.
  2. 将对象序列化为JSON。
  3. Base64 encode the JSON.
  4. Base64编码的JSON。
  5. Use the Base64 encoded string as the cookie value.
  6. 使用Base64编码的字符串作为cookie值。

Just reverse the process when reading the cookie.

在读取cookie时,只需反转这个过程。

You have to use Version 1 cookies because Base64 character sets needs to be quoted. If you want use old Netscape style cookie, you need to use an URL-safe Base64 encoder.

您必须使用版本1 cookie,因为需要引用Base64字符集。如果您想使用旧的Netscape风格的cookie,您需要使用url安全的Base64编码器。

Of course, this is only good for small objects.

当然,这只适用于小对象。

#3


1  

You will need to serialize your object and then write it out as text. I would consider using JSON as it's well supported.

您将需要序列化对象,然后将其写入文本。我将考虑使用JSON,因为它得到了很好的支持。

There is a good parser here. You will just need to call the JSON.stringify() method. To write cookies in javascript you need to append a string in the correct format to

这里有一个很好的解析器。您只需要调用JSON.stringify()方法。要用javascript编写cookie,需要以正确的格式附加一个字符串到

window.document.cookie

The string should be in the following format

字符串应该是以下格式。

'name=cookiename; expires=Thu, 2 Aug 2001 20:47:11 UTC; path=/'

#4


0  

Cookies store only string values so you need to serialize your object to string somehow. And deserialize it when you will read it from the cookie. However this can work only if your object has some simple data (strings, numbers, arrays), and will not work with functions for sure. I'm also not sure why you want to do that.

cookie只存储字符串值,因此需要以某种方式将对象序列化为字符串。当你从cookie中读取时,反序列化它。但是,只有当对象有一些简单的数据(字符串、数字、数组),并且不能肯定地与函数一起工作时,这种方法才有效。我也不知道你为什么要这么做。

#5


0  

Cookies are designed to only hold text, so if you need serialize your object into a simple string for this to work.

cookie被设计成只保存文本,所以如果您需要将对象序列化为一个简单的字符串,以使其工作。

On most browsers cookies are limited to +- 4096 bytes so you can't store much information.

在大多数浏览器中,cookie被限制为+- 4096字节,所以您不能存储太多信息。

#6


0  

Serializing Javascript Objects into Cookies

将Javascript对象序列化为cookie

var expires = new Date();
expires.setDate(expires.getDate() + 7); // 7 = days to expire
document.cookie = "logintoken=somelogintoken;expires=" + expires.toGMTString();

Read JavaScript Cookies also.

读取JavaScript饼干。

#7


-2  

i think only 4kb is allowd to a cookie

我认为一块饼干只允许4kb

#1


6  

If you can serialize your object into its canonical string representation, and can unserialize it back into its object form from said string representation, then yes you can put it into a cookie.

如果您可以将对象序列化为其规范的字符串表示形式,并可以从上述字符串表示形式将其反序列化为对象形式,那么您可以将其放入cookie中。

Judging from your earlier question regarding storing the result of window.open() into a cookie, this is not the answer you are hoping for.

从前面关于将window.open()的结果存储到cookie中的问题来看,这并不是您希望的答案。

#2


2  

This is one way to do it,

这是一种方法,

  1. Serialize your object into JSON.
  2. 将对象序列化为JSON。
  3. Base64 encode the JSON.
  4. Base64编码的JSON。
  5. Use the Base64 encoded string as the cookie value.
  6. 使用Base64编码的字符串作为cookie值。

Just reverse the process when reading the cookie.

在读取cookie时,只需反转这个过程。

You have to use Version 1 cookies because Base64 character sets needs to be quoted. If you want use old Netscape style cookie, you need to use an URL-safe Base64 encoder.

您必须使用版本1 cookie,因为需要引用Base64字符集。如果您想使用旧的Netscape风格的cookie,您需要使用url安全的Base64编码器。

Of course, this is only good for small objects.

当然,这只适用于小对象。

#3


1  

You will need to serialize your object and then write it out as text. I would consider using JSON as it's well supported.

您将需要序列化对象,然后将其写入文本。我将考虑使用JSON,因为它得到了很好的支持。

There is a good parser here. You will just need to call the JSON.stringify() method. To write cookies in javascript you need to append a string in the correct format to

这里有一个很好的解析器。您只需要调用JSON.stringify()方法。要用javascript编写cookie,需要以正确的格式附加一个字符串到

window.document.cookie

The string should be in the following format

字符串应该是以下格式。

'name=cookiename; expires=Thu, 2 Aug 2001 20:47:11 UTC; path=/'

#4


0  

Cookies store only string values so you need to serialize your object to string somehow. And deserialize it when you will read it from the cookie. However this can work only if your object has some simple data (strings, numbers, arrays), and will not work with functions for sure. I'm also not sure why you want to do that.

cookie只存储字符串值,因此需要以某种方式将对象序列化为字符串。当你从cookie中读取时,反序列化它。但是,只有当对象有一些简单的数据(字符串、数字、数组),并且不能肯定地与函数一起工作时,这种方法才有效。我也不知道你为什么要这么做。

#5


0  

Cookies are designed to only hold text, so if you need serialize your object into a simple string for this to work.

cookie被设计成只保存文本,所以如果您需要将对象序列化为一个简单的字符串,以使其工作。

On most browsers cookies are limited to +- 4096 bytes so you can't store much information.

在大多数浏览器中,cookie被限制为+- 4096字节,所以您不能存储太多信息。

#6


0  

Serializing Javascript Objects into Cookies

将Javascript对象序列化为cookie

var expires = new Date();
expires.setDate(expires.getDate() + 7); // 7 = days to expire
document.cookie = "logintoken=somelogintoken;expires=" + expires.toGMTString();

Read JavaScript Cookies also.

读取JavaScript饼干。

#7


-2  

i think only 4kb is allowd to a cookie

我认为一块饼干只允许4kb