如何保存在多个html页面中使用的javascript变量的状态 [英] How to save the state of a javascript variable that is used in multiple html pages

查看:352
本文介绍了如何保存在多个html页面中使用的javascript变量的状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在phonegap中提出申请。我做了一个custom.js的JavaScript文件,它有一些功能如

I am trying to make an application in phonegap. I have made a custom.js javascript file which have some functions as

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

html页面。在第一个HTML页面,我使用一个变量在func1这是做一些操作。在第二页中,我想在func2中访问它处于func1的状态。但我无法做到。我在两个html页面中包含custom.js。我已经阅读的javascript文件得到重置/刷新时使用在多个页面。任何人都可以给我一个例子,如何保存func1中的变量状态,然后访问func2(在不同的HTML页面)在他在func1的状态下的变量。我也读过关于视图状态。但它不是为我工作。请帮助...

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...

推荐答案

将值存储在 localstorage 并从那里引用它。

Store the values in localstorage and reference it from there.

function first() {
    localStorage.setItem('myItem', "something you want to store");
}

function second() {
    myValue = null;
    if (localStorage.getItem('myItem')) {
        myValue = localStorage.getItem('myItem');
    }
}

这篇关于如何保存在多个html页面中使用的javascript变量的状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆