【问题标题】:Accessing variables between javascript files [duplicate]访问javascript文件之间的变量[重复]
【发布时间】:2019-08-08 17:01:33
【问题描述】:

我正在创建一个网络应用程序,其中一个对象 property 在单击 inputs.html 页面中的按钮时创建。但是,results.html 需要访问创建的 property 以提示用户将其保存在 firestore 中。

  • 我已经尝试过导入和导出变量,但出现此错误:Uncaught SyntaxError: Unexpected token )。
  • 我已尝试在 inputs.html 中创建一个 const 变量属性,该属性使用我的脚本进行更改,但我无法在 results.html 中访问它.
  • 我还在 results.html 中创建了一个脚本标记,链接到创建 property 的 javascript 文件。

这里是 inputs.js 用于创建 property 并在 inputs.html 中导入。

//create a property and go to results page
document.getElementById("createProperty").addEventListener("click", ()=>{

    property= new Property( document.getElementById("address").value, ..., );

    window.location = "../Frontend/results.html";
});
  }

这是 results.js 中调用 property 并位于 results.html 页面中的代码。

document.getElementById("test").addEventListener("click",(e)=>{

    //example of a possible output
    console.log(property.address); 
});

我希望控制台记录 askprice。但是,收到的错误是:Uncaught ReferenceError: property is not defined。

我想知道可以使用哪些方法来访问这些 javascrip 文件之间的 property

【问题讨论】:

    标签: javascript html


    【解决方案1】:

    我想知道可以使用哪些方法来访问这些 javascrip 文件之间的属性。

    您不能在页面加载之间共享任何 JavaScript 状态,包括变量。变量property不能从一页持续到下一页。

    相反,您需要将数据推送到某种持久性存储中,例如localStoragesessionStorage,或者通过查询字符串之类的方式将其传递到下一页,即results.html?property=askprice

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-04-11
      • 2015-02-22
      • 2011-08-25
      • 2017-06-03
      • 1970-01-01
      • 2013-10-31
      • 2011-05-16
      相关资源
      最近更新 更多