Javascript存储/全局数据/对象的状态 [英] Javascript storing/state of global data/object
问题描述
有没有办法在窗口
对象中存储全局数据,以便数据可以在页面重新加载/刷新时生存。所以让我说我分配我的全局数据/对象 -
Is there a way to store global data in the window
object such that the data can survive page reloads/refresh. So lets say I assign my global data/object -
window.myObject = myProductObject
用户刷新/重新加载页面,或者可能跳转到我网站的另一个页面。
And the user refreshes/reloads the page or may be jumps to another page of my website. Is window.myObject
still available after the page reload?
注意 - :我 window.myObject 不能将对象存储在cookie中,因为它是一个对象,我的意思是它可以是另一个自定义对象的引用,或者可以引用另一个通过window.open打开的window对象
NOTE -: I cannot store the object in a cookie, since its a object, I mean it could be a reference to another custom object or it could refer to another "window" object which has opened via "window.open"
推荐答案
使用 window.top.name
hack
var data = window.top.name ? JSON.parse(window.top.name) : {}
...
window.top.name = JSON.stringify(data)
window.top.name
跨页面加载
我建议您使用像草坪的抽象,而不是
I recommend you use an abstraction like lawnchair instead though
这篇关于Javascript存储/全局数据/对象的状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!