简体   繁体   English

如何在其他页面上保留JavaScript值

[英]How to retain a JavaScript value on a different page

I have a function that get a value that needs to be accesed across multiple pages (function below). 我有一个函数,该函数获取需要跨多个页面访问的值(下面的函数)。

var currentProperty = '';
function goTo(ele){
    var firstLine = ele.getAttribute('value');
    var user = firebase.auth().currentUser;
    var ref = firebase.database().ref().child("users").child(user.uid).child('rentingAddresses');

    ref.orderByChild("firstLine").equalTo(firstLine).once("value", function(snapshot) {
      snapshot.forEach(function(child) {
        currentProperty = child.key;
        console.log(currentProperty);
      });
    });
}

When the function is fired, it saves the value into the variable but when I access it on another page the value has gone. 当函数被触发时,它将值保存到变量中,但是当我在另一页上访问它时,该值消失了。 I've tried using the following method: 我尝试使用以下方法:

window.onload = function() {
    var currentProperty = 'nothing'
    localStorage.setItem("prop", currentProperty);
}
function goTo(ele){
    var firstLine = ele.getAttribute('value');
    var user = firebase.auth().currentUser;
    var ref = firebase.database().ref().child("users").child(user.uid).child('rentingAddresses');

    ref.orderByChild("firstLine").equalTo(firstLine).once("value", function(snapshot) {
          snapshot.forEach(function(child) {
          currentProperty = child.key;
          console.log(currentProperty);
      });
    });
}

But it doesnt seem to work either. 但这似乎也不起作用。 Is there any other way to retain the value once I leave the page containing the function? 一旦离开包含函数的页面,还有其他方法可以保留该值吗?

TYIA. TYIA。

There are a few problems in your code: 您的代码中存在一些问题:

  1. You're never setting the value that you read from the database into local storage. 您永远不会将从数据库读取的值设置为本地存储。
  2. You're never reading the value from local storage in the code you provided. 您永远不会在提供的代码中从本地存储中读取值。

setting the value that you read from the database into local storage 将您从数据库读取的值设置为本地存储

You're only setting the value to local storage once in: 您只需在以下位置将值设置为本地存储即可:

window.onload = function() {
  var currentProperty = 'nothing'
  localStorage.setItem("prop", currentProperty);
}

This means that every time the page loads you set prop='nothing' in local storage. 这意味着每次页面加载时,您在本地存储中设置prop='nothing' Nothing else ever happens. 什么都没有发生。

You'll probably also want to write the value that you read from the database into local storage. 您可能还希望将从数据库中读取的值写入本地存储。 In that case, you should do so in the callback from your once("value" handler: 在这种情况下,应在once("value"处理程序的回调中进行此操作:

ref.orderByChild("firstLine").equalTo(firstLine).once("value", function(snapshot) {
  snapshot.forEach(function(child) {
    currentProperty = child.key;
    localStorage.setItem("prop", currentProperty);
  });
});

With this change you'll be updating the local storage to the value that you read from the database. 进行此更改后,您将把本地存储更新为从数据库读取的值。

reading the value from local storage in the next page 在下一页从本地存储中读取值

You'll need to read the value back from local storage in the next page. 您需要在下一页从本地存储中读取该值。 Given the code you've already shown, I expect you'll do this with something like: 给定您已经显示的代码,我希望您可以使用类似的方法做到这一点:

window.onload = function() {
  var currentProperty = localStorage.getItem("prop");
  // TODO: do something with currentProperty
}

Note that could also simply re-read the value from the database and using it: 注意,这也可以简单地从数据库中重新读取值并使用它:

ref.orderByChild("firstLine").equalTo(firstLine).once("value", function(snapshot) {
  snapshot.forEach(function(child) {
    currentProperty = child.key;
    // TODO: do something with currentProperty
  });
});

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

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