簡體   English   中英

如何在用戶離線時將 xml 文件保存到本地存儲並更新值?

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

我正在嘗試編寫貨幣轉換器。 我正在從存儲貨幣和匯率的 url 下載 xml 文件。 我想將貨幣保存到本地存儲,這樣當用戶離線時,他仍然可以進行轉換。 我在控制台中打印 xml 文件只是為了檢查我是否正在獲取 xml 文件。 所以我的問題是,我如何將貨幣和匯率存儲到本地存儲,以及如何在用戶再次在線時更新這些值? 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();
}

xml 文件的 url 顯示在代碼中。

是的,正如 Chris 已經說過的,您可以使用默認的 localStorage:

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

例如,在你得到回應之后

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