简体   繁体   English

如何保存多个html页面中使用的javascript变量的状态

[英]How to save the state of a javascript variable that is used in multiple html pages

I am trying to make an application in phonegap. 我正在尝试在phonegap中创建一个应用程序。 I have made a custom.js javascript file which have some functions as 我已经制作了一个custom.js javascript文件,其中包含一些函数

function func1(){....}
function func2(){....}

All these functions will be used in two different html pages. 所有这些功能都将用于两个不同的html页面。 In first HTML page,I am using a variable in func1 which is doing some operation. 在第一个HTML页面中,我在func1中使用了一个变量,它正在进行一些操作。 In second page, I want to access it in func2 in the state in which he was in func1. 在第二页中,我想在func2中以func1的状态访问它。 But i am unable to do it. 但我无法做到。 I am including custom.js in both html pages. 我在两个html页面中都包含custom.js. I have read that javascript files get reset/refresh when used in multiple pages. 我已经读过,当在多个页面中使用时,javascript文件会被重置/刷新。 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. 任何人都可以给我一个例子,说明如何在func1中保存变量的状态,然后在func1中的func2(在不同的HTML页面中)访问该变量。 i have also read about view state. 我也读过有关视图状态的内容。 but it is not working either for me. 但它对我来说都不起作用。 Please help... 请帮忙...

Store the values in localstorage and 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');
    }
}

in modern browsers you can use localStorage for 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将值存储到大多数现代浏览器的本地存储中。

The easier and preferred method would be to use window.localStorage for storing site wide variables. 更简单和首选的方法是使用window.localStorage存储站点范围的变量。 To accommodate older browsers as well, you can maybe consider having cookies as a secondary storage location. 为了适应旧版浏览器,您可以考虑将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];
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM