如何保存在多个 html 页面中使用的 javascript 变量的状态
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12895754/
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
How to save the state of a javascript variable that is used in multiple html pages
提问by Shahid Iqbal
I am trying to make an application in phonegap. I have made a custom.js javascript file which have some functions as
我正在尝试在 phonegap 中创建一个应用程序。我制作了一个 custom.js javascript 文件,它有一些功能
function func1(){....}
function func2(){....}
All these functions will be used in two different html pages. In first HTML page,I am using a variable in func1 which is doing some operation. In second page, I want to access it in func2 in the state in which he was in func1. But i am unable to do it. I am including custom.js in both html pages. I have read that javascript files get reset/refresh when used in multiple pages. Can anybody give me an example that how to save the state of variable in func1 and then access that variable in func2 (in different HTML page) in the state in which he was in func1. i have also read about view state. but it is not working either for me. Please help...
所有这些函数都将用于两个不同的 html 页面。在第一个 HTML 页面中,我在 func1 中使用了一个正在执行某些操作的变量。在第二页中,我想以他在 func1 中的状态在 func2 中访问它。但我做不到。我在两个 html 页面中都包含 custom.js。我读过 javascript 文件在多个页面中使用时会重置/刷新。谁能给我一个例子,说明如何在func1中保存变量的状态,然后在func1中的状态下访问func2中的变量(在不同的HTML页面中)。我还阅读了有关视图状态的信息。但它对我也不起作用。请帮忙...
回答by epascarello
Store the values in localstorageand reference it from there.
将值存储在localstorage 中并从那里引用它。
function first() {
localStorage.setItem('myItem', "something you want to store");
}
function second() {
myValue = null;
if (localStorage.getItem('myItem')) {
myValue = localStorage.getItem('myItem');
}
}
回答by lrsjng
in modern browsers you can use localStoragefor that
在现代浏览器,你可以使用localStorage的为
var get = function (key) {
return window.localStorage ? window.localStorage[key] : null;
}
var put = function (key, value) {
if (window.localStorage) {
window.localStorage[key] = value;
}
}
use get and put to store value to the local storage of most modern browsers..
使用 get 和 put 将值存储到大多数现代浏览器的本地存储中。
回答by techfoobar
The easier and preferred method would be to use window.localStorage
for storing site wide variables. To accommodate older browsers as well, you can maybe consider having cookiesas a secondary storage location.
更简单和首选的方法是window.localStorage
用于存储站点范围的变量。为了也适应旧浏览器,您可以考虑将cookie作为辅助存储位置。
// credits to http://mathiasbynens.be/notes/localstorage-pattern
var hasStorage = (function() {
try {
localStorage.setItem(mod, mod);
localStorage.removeItem(mod);
return true;
} catch(e) {
return false;
}
}());
function setSiteWideValue(_key, _value) {
if(hasStorage) {
localStorage[_key] = _value;
}
else {
document.cookie = _key+'='+_value+'; expires=<date-here>; path=/; ;domain=<.yourdomain>';
}
}
function getSiteWideValue(_key) {
if(hasStorage) {
return localStorage[_key];
}
else {
if(document.cookie.indexOf(_key+'=') != -1) {
return document.cookie.split(_key+'=')[1].split(';')[0];
}
}
}