简体   繁体   English

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

[英]can we assign object into cookie in javascript?

Does anyone know is it possible to assign an object into cookies in javascript? 有谁知道可以在javascript中将对象分配到cookie中吗? If yes, how can we do it? 如果是,我们该怎么做?

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中的问题来判断,这不是您希望的答案。

This is one way to do it, 这是一种方法,

  1. Serialize your object into JSON. 将对象序列化为JSON。
  2. Base64 encode the JSON. Base64编码JSON。
  3. Use the Base64 encoded string as the cookie value. 使用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. 您必须使用版本1 cookie,因为需要引用Base64字符集。 If you want use old Netscape style cookie, you need to use an URL-safe Base64 encoder. 如果要使用旧的Netscape样式cookie,则需要使用URL安全的Base64编码器。

Of course, this is only good for small objects. 当然,这只适用于小物件。

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. 您只需要调用JSON.stringify()方法。 To write cookies in javascript you need to append a string in the correct format to 要在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=/'

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 Cookie

Cookies store only string values so you need to serialize your object to string somehow. Cookies只存储字符串值,因此您需要以某种方式将对象序列化为字符串。 And deserialize it when you will read it from the cookie. 当你从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. 我也不确定你为什么要那样做。

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字节,因此您无法存储太多信息。

我认为只允许4kb的cookie

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM