页面刷新后保留 JavaScript 变量值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11841771/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 07:19:40  来源:igfitidea点击:

Keep a JavaScript variables value after a page refresh?

javascriptjquery

提问by Anudeep GI

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?

我正在处理一个项目,其中变量被分配了一个值,刷新浏览器时我需要该值。有什么方法可以获得该值?

回答by Igor Chubin

Cookies

饼干

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。cookie,也称为 HTTP cookie、网络 cookie 或浏览器 cookie,通常是从网站发送的一小段数据,并在用户浏览网站时存储在用户的网络浏览器中(来自维基百科)。

Using a cookie you can save the stateand later read it and use it.

使用 cookie,您可以保存状态,然后读取并使用它。

With jQuery it is very easy to use cookies.

使用 jQuery,使用 cookie 非常容易。

To set:

设置:

$.cookie("var", "10");

To get:

要得到:

$.cookie("var")

To delete:

删除:

$.cookie("var", null);

Local Storage

本地存储

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.

当您想在本地保存大量数据时,您还有另一个机会——使用本地存储(自 HTML5 起)。您可以直接使用 JavaScript 或使用可用的 jQuery 插件之一来完成。

for example, with totalStorage:

例如,使用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);

回答by Eric J.

You can place that value in a cookie.

您可以将该值放在 cookie 中。

The complete list of alternatives is

完整的替代清单是

  • Standard HTTP cookies
  • Local Shared Objects (Flash cookies)
  • Silverlight Isolated Storage
  • Storing cookies in RGB values of auto-generated, force-cached PNGs using HTML5 Canvas ag to read pixels (cookies) back out
  • Storing cookies in Web history
  • Storing cookies in HTTP ETags
  • Storing cookies in Web cache
  • window.name caching
  • Internet Explorer userData storage
  • HTML5 Session Storage
  • HTML5 Local Storage
  • HTML5 Global Storage
  • HTML5 Database Storage via SQLite
  • 标准 HTTP cookie
  • 本地共享对象(Flash cookie)
  • Silverlight 独立存储
  • 使用 HTML5 Canvas ag 将 cookie 存储在自动生成的、强制缓存的 PNG 的 RGB 值中以读取像素(cookie)
  • 在网络历史记录中存储 cookie
  • 在 HTTP ETag 中存储 cookie
  • 在 Web 缓存中存储 cookie
  • window.name 缓存
  • Internet Explorer 用户数据存储
  • HTML5 会话存储
  • HTML5 本地存储
  • HTML5 全局存储
  • 通过 SQLite 存储 HTML5 数据库

Evercookie

永远的饼干

回答by Jan Han?i?

You can store it in a cookie. Or use local storage if supported by the browser.

您可以将其存储在 cookie 中。或者,如果浏览器支持,则使用本地存储。

回答by FredrikO

You can use $.cookie("key", value)to set the value, and $.cookie("key")to get the saved value.

您可以使用$.cookie("key", value)来设置值,并$.cookie("key")获取保存的值。

EDIT:A small tutorial on how you use cookies in jQuery: http://www.electrictoolbox.com/jquery-cookies/

编辑:关于如何在 jQuery 中使用 cookie 的小教程:http: //www.electrictoolbox.com/jquery-cookies/