纯Javascript-在cookie中存储对象
没有jQuery 我想在cookie中存储一个对象或数组 该对象在页面刷新后应可用 如何使用纯JavaScript实现这一点?我读了很多文章,但不知道如何适当地序列化纯Javascript-在cookie中存储对象,javascript,object,cookies,Javascript,Object,Cookies,没有jQuery 我想在cookie中存储一个对象或数组 该对象在页面刷新后应可用 如何使用纯JavaScript实现这一点?我读了很多文章,但不知道如何适当地序列化 编辑: 代码: 如何序列化实例 如何维护功能,但更改实例的结构,以便能够使用stringify进行序列化 如果您可以将对象序列化为其规范字符串表示形式,并且可以从所述字符串表示形式将其反序列化为其对象形式,则可以将其放入cookie中。如果对象提供有意义的序列化或JSON.stringify()则使用对象自己的.toString
编辑: 代码:
实例
stringify
进行序列化如果您可以将对象序列化为其规范字符串表示形式,并且可以从所述字符串表示形式将其反序列化为其对象形式,则可以将其放入cookie中。如果对象提供有意义的序列化或
JSON.stringify()
则使用对象自己的.toString()
方法。但是,请注意,cookie通常长度有限,无法保存大量数据。尝试编写该cookie
function bake_cookie(name, value) {
var cookie = [name, '=', JSON.stringify(value), '; domain=.', window.location.host.toString(), '; path=/;'].join('');
document.cookie = cookie;
}
阅读它需要:
function read_cookie(name) {
var result = document.cookie.match(new RegExp(name + '=([^;]+)'));
result && (result = JSON.parse(result[1]));
return result;
}
要删除它,请执行以下操作:
function delete_cookie(name) {
document.cookie = [name, '=; expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/; domain=.', window.location.host.toString()].join('');
}
要序列化复杂对象/实例,为什么不在实例中编写数据转储函数:
function userConstructor(name, street, city) {
// ... your code
this.dumpData = function() {
return {
'userConstructorUser': {
name: this.name,
street: this.street,
city: this.city
}
}
}
然后转储数据,对其进行字符串化,将其写入cookie,下次要使用它时,只需执行以下操作:
var mydata = JSON.parse(read_cookie('myinstances'));
new userConstructor(mydata.name, mydata.street, mydata.city);
cookie自适应类来自: 您所需要做的就是设置和获取需要存储在cookie中的变量 使用:int、字符串、数组、列表、复杂对象 例如:
var toStore = Session.get('toStore');
if (toStore == undefined)
toStore = ['var','var','var','var'];
else
console.log('Restored from cookies'+toStore);
Session.set('toStore', toStore);
类别:
// Cross reload saving
if (JSON && JSON.stringify && JSON.parse) var Session = Session || (function() {
// session store
var store = load();
function load()
{
var name = "store";
var result = document.cookie.match(new RegExp(name + '=([^;]+)'));
if (result)
return JSON.parse(result[1]);
return {};
}
function Save() {
var date = new Date();
date.setHours(23,59,59,999);
var expires = "expires=" + date.toGMTString();
document.cookie = "store="+JSON.stringify(store)+"; "+expires;
};
// page unload event
if (window.addEventListener) window.addEventListener("unload", Save, false);
else if (window.attachEvent) window.attachEvent("onunload", Save);
else window.onunload = Save;
// public methods
return {
// set a session variable
set: function(name, value) {
store[name] = value;
},
// get a session value
get: function(name) {
return (store[name] ? store[name] : undefined);
},
// clear session
clear: function() { store = {}; }
};
})();
将对象转换为json并存储在cookie中。
stringify
不起作用。我得到TypeError:将循环结构转换为JSON
我的代码是:var instances={};实例[strInstanceId]。插件=oPlugin代码>谢谢,如果我让stringify
工作,我会同意的。您能检查一下我的代码问题吗?JSON无法序列化整个实例,但您可以序列化这些实例中可能需要的任何相关数据,并使用保存的数据重新初始化它们。例如,您有一个实例,其中name='johndoe',您可以在该实例上编写一个函数以返回类似{myinstance:{name:'johndoe'}}的对象,然后使用此数据再次调用构造函数。除此之外,你可以试试GSerializer:(这是一篇很好的帖子),但我建议你不要这样做,因为饼干的尺寸有限。。。。。。而且cookie太大可能会导致用户永久性站点失败,而您不会注意到它,因为上游标头太大。我编辑了我的帖子,所以你可以看到我上面的评论是什么意思。我不明白你的转储功能的答案。变量instances
包含两个导致此错误的对象,一个是ExtJS,另一个是DeftJS对象。如何序列化这些文件?你能解释一下吗?每个JS实例中通常有两种类型的数据:和。我的评论的意思是,在cookie中存储函数是没有用的,因为这些函数通常不会更改。您应该提取属性并将它们写入cookie,因为存储大型实例的实例(我猜字符串化的ExtJS和DeftJS实例将非常庞大)不适合cookie(大小限制基本上是4KB)。。。
// Cross reload saving
if (JSON && JSON.stringify && JSON.parse) var Session = Session || (function() {
// session store
var store = load();
function load()
{
var name = "store";
var result = document.cookie.match(new RegExp(name + '=([^;]+)'));
if (result)
return JSON.parse(result[1]);
return {};
}
function Save() {
var date = new Date();
date.setHours(23,59,59,999);
var expires = "expires=" + date.toGMTString();
document.cookie = "store="+JSON.stringify(store)+"; "+expires;
};
// page unload event
if (window.addEventListener) window.addEventListener("unload", Save, false);
else if (window.attachEvent) window.attachEvent("onunload", Save);
else window.onunload = Save;
// public methods
return {
// set a session variable
set: function(name, value) {
store[name] = value;
},
// get a session value
get: function(name) {
return (store[name] ? store[name] : undefined);
},
// clear session
clear: function() { store = {}; }
};
})();