页面刷新后保留JavaScript变量值? [英] Keep a JavaScript variables value after a page refresh?
问题描述
我正在处理一个项目,其中为变量分配了一个值,当浏览器刷新时我需要该值。是否有任何方法可以获得该值?
i am working with one project in which variable is assigned a value i need that value when browser is refreshed.Is there any way get that value?
推荐答案
Cookies
最好解决方案是使用 cookies 。 Cookie(也称为HTTP Cookie,网络Cookie或浏览器Cookie)通常是从网站发送的一小段数据,并在用户浏览网站时存储在用户的网络浏览器中(来自维基百科)。
The best solution is to use cookies. A cookie, also known as an HTTP cookie, web cookie, or browser cookie, is usually a small piece of data sent from a website and stored in a user's web browser while a user is browsing a website (from Wikipedia).
使用cookie可以保存州,然后阅读并使用它。
Using a cookie you can save the state and later read it and use it.
使用jQuery可以很容易地使用cookie。
With jQuery it is very easy to use cookies.
设置:
$.cookie("var", "10");
获取:
$.cookie("var")
要删除:
$.cookie("var", null);
本地存储空间
当您想要保存大量数据时,您还有另一个机会 - 使用本地存储(自HTML5起)。您可以使用JavaScript或使用可用的jQuery插件直接执行此操作。
When you want to save localy great amount of data, you have another opportunity — to use local storage (since HTML5). You can do it directly using JavaScript or using one of available jQuery plugins.
例如, totalStorage
:
var scores = new Array();
scores.push({'name':'A', points:10});
scores.push({'name':'B', points:20});
scores.push({'name':'C', points:0});
$.totalStorage('scores', scores);
这篇关于页面刷新后保留JavaScript变量值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!