简体   繁体   English

如何在用户离线时将 xml 文件保存到本地存储并更新值?

[英]How do I save xml file to local storage and update values when user is offline?

I am trying to code a currency converter.我正在尝试编写货币转换器。 I am downloading the xml file from a url where the currencies and the rates are stored there.我正在从存储货币和汇率的 url 下载 xml 文件。 I want to save currencies to local storage so when the user is offline he will still be able to do conversion.我想将货币保存到本地存储,这样当用户离线时,他仍然可以进行转换。 I print the xml file in the console just to check if I am getting the xml file.我在控制台中打印 xml 文件只是为了检查我是否正在获取 xml 文件。 So my question is, how do i store the currencies and the rates to local storage, and how do i update the values when the user again online?所以我的问题是,我如何将货币和汇率存储到本地存储,以及如何在用户再次在线时更新这些值? javascript: javascript:

function display(e ) {
    if ((e.target !== e.currentTarget) && e.target.id !== "clear" && e.target.id !== "kati") {
        var clickedItem = e.target.id;
        document.getElementById("display").value =  document.getElementById("display").value + clickedItem;
    }
    else  if (e.target.id = "clear" && e.target.id !== "kati") {
        document.getElementById("display").value = null;
    } else if (e.target.id = "kati") {
        var ourRequest = new XMLHttpRequest();
        ourRequest.open('GET', 'https://devweb2018.cis.strath.ac.uk/~aes02112/ecbxml.php');
        ourRequest.onreadystatechange = function () {
            var data = ourRequest.responseText;
            console.log(data);
        };
        ourRequest.send();
    }
    e.stopPropagation();
}

The url for the xml file is displayed in the code. xml 文件的 url 显示在代码中。

Yes, as Chris has already said, you can use default localStorage:是的,正如 Chris 已经说过的,您可以使用默认的 localStorage:

localStorage.setItem("hello","world");
localStorage.getItem("hello"); //world

For example, after you've got response例如,在你得到回应之后

ourRequest.onreadystatechange = function () {
 if (ourRequest.readyState === 4) {
  if (xhr.status == 200){
   if (ourRequest.status == 200){
     var data = ourRequest.responseXML;
     var someTag = data.getElementsByTagName("someTag");
     //something like this
     localStorage.setItem("someTagValue",someTag[0].childNodes[0].nodeValue);
   }
  }    
};

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

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