全球数据/对象Javascript的存储/状态 [英] Javascript storing/state of global data/object

查看:138
本文介绍了全球数据/对象Javascript的存储/状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有存储在全局数据的方式窗口对象以使得数据能够生存页面重新加载/刷新。因此,可以说我分配我的全球数据/对象 -

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

和用户刷新/重新加载页面或可跳转到自己的网站的另一个页面。为 window.myObject 的页面重新加载后仍然可用?

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?

注意 - :我不能在对象存储在cookie中,因为它的一个对象,我的意思是它可能是另一个自定义对象的引用,也可能指到另一个的窗口对象,它具有通过开设window.open

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 破解

var data = window.top.name ? JSON.parse(window.top.name) : {}
...
window.top.name = JSON.stringify(data)

window.top.name 整个页面加载仍然存在。

window.top.name persists across page loads

我建议你使用像 lawnchair <​​/A>的抽象,而不是虽然

I recommend you use an abstraction like lawnchair instead though

这篇关于全球数据/对象Javascript的存储/状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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