Javascript 跨页面保留javascript变量?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1981673/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Persist javascript variables across pages?
提问by deostroll
Is there a way we can persist javascript variables across various pages? Suppose in Page A I am setting window.someVar = 5. Then I move to Page B, via clicking a hyperlink in A, and do something like alert(window.someVar)-- I should get a message box displaying 5. Is there a technique to persist someVaras such...?
有没有办法可以在各个页面上持久保存 javascript 变量?假设在 Page AI 中设置window.someVar = 5. 然后我移动到页面 B,通过单击 A 中的一个超链接,并执行类似的操作alert(window.someVar)——我应该得到一个显示 5 的消息框。是否有一种技术可以保持someVar这种状态......?
回答by Gumbo
You could use the window's name window.nameto store the information. This is known as JavaScript session. But it only works as long as the same window/tab is used.
您可以使用窗口的名称window.name来存储信息。这称为JavaScript 会话。但它只有在使用相同的窗口/选项卡时才有效。
回答by Andy Hume
For completeness, also look into the local storage capabilities & sessionStorage of HTML5. These are supported in the latest versions of all modern browsers, and are much easier to use and less fiddly than cookies.
为了完整起见,还可以查看 HTML5 的本地存储功能和 sessionStorage。这些在所有现代浏览器的最新版本中都受支持,并且比 cookie 更易于使用且不那么繁琐。
http://www.w3.org/TR/2009/WD-webstorage-20091222/
http://www.w3.org/TR/2009/WD-webstorage-20091222/
https://www.w3.org/TR/webstorage/. (second edition)
https://www.w3.org/TR/webstorage/。(第二版)
Here are some sample code for setting and getting the values using sessionStorage and localStorage :
以下是使用 sessionStorage 和 localStorage 设置和获取值的一些示例代码:
// HTML5 session Storage
sessionStorage.setItem("variableName","test");
sessionStorage.getItem("variableName");
//HTML5 local storage
localStorage.setItem("variableName","Text");
// Receiving the data:
localStorage.getItem("variableName");
回答by CMS
I would recommend you to give a look to this library:
我建议你看看这个库:
I really like it, it supports a variety of storage backends (from cookies to HTML5 storage, Gears, Flash, and more...), its usage is really transparent, you don't have to know or care which backend is used the library will choose the right storage backend depending on the browser capabilities.
我真的很喜欢它,它支持多种存储后端(从 cookie 到 HTML5 存储、Gears、Flash 等等...),它的使用非常透明,您不必知道或关心使用了哪个后端library 将根据浏览器功能选择正确的存储后端。
回答by Pekka
Yes, using Cookies. But be careful, don't put too much in them (I think there is a limit at 4kb). But a few variables are ok.
是的,使用Cookies。但是要小心,不要放太多(我认为有 4kb 的限制)。但是有几个变量是可以的。
If you need to store considerably more than that, check out @Annie's great tips in the other answer. For small time data storage, I would say Cookies are the easiest thing.
如果您需要存储更多,请查看@Annie 在另一个答案中的重要提示。对于小时间数据存储,我会说 Cookies 是最简单的事情。
Note that cookies are stored client side.
请注意,cookie 存储在客户端。
回答by Annie
You can persist values using HTML5 storage, Flash Storage, or Gears. The dojo storagelibrary provides a nice wrapper for this.
您可以使用 HTML5 存储、闪存存储或 Gears 保留值。该道场存储库提供了一个很好的包装这一点。
回答by ElectroBit
I recommend web storage. Example:
我推荐网络存储。例子:
// Storing the data:
localStorage.setItem("variableName","Text");
// Receiving the data:
localStorage.getItem("variableName");
// Storing the data:
localStorage.setItem("variableName","Text");
// Receiving the data:
localStorage.getItem("variableName");
Just replace variablewith your variable name and textwith what you want to store. According to W3Schools, it's better than cookies.
只需替换variable为您的变量名称和text您要存储的内容。根据 W3Schools 的说法,它比饼干更好。
回答by Adrian May
You can use http://rhaboo.orgas a wrapper around localStorage. It stores complex objects but doesn't merely stringify and parse the whole thing like most such libraries do. That's really inefficient if you want to store a lot of data and add to it or change it in small chunks. Also, JSON discards a lot of important stuff like non-numerical properties of arrays.
您可以使用http://rhaboo.org作为 localStorage 的包装器。它存储复杂的对象,但不仅仅是像大多数此类库那样对整个事物进行字符串化和解析。如果您想存储大量数据并将其添加到其中或以小块的形式更改它,那确实是低效的。此外,JSON 丢弃了很多重要的东西,比如数组的非数字属性。
In rhaboo you can write things like this:
在 rhaboo 中,你可以这样写:
var store = Rhaboo.persistent('Some name');
store.write('count', store.count ? store.count+1 : 1);
var laststamp = store.stamp ? store.stamp.toString() : "never";
store.write('stamp', new Date());
store.write('somethingfancy', {
one: ['man', 'went'],
2: 'mow',
went: [ 2, { mow: ['a', 'meadow' ] }, {} ]
});
store.somethingfancy.went[1].mow.write(1, 'lawn');
console.log( store.somethingfancy.went[1].mow[1] ); //says lawn
BTW, I wrote rhaboo
顺便说一句,我写了 rhaboo

