用Cookie日志记录替换InnerHTML

时间:2019-09-11 23:31:20

标签: javascript html ajax replace element

我有一些javascript代码,可用来在单击时替换元素的innerHTML。现在,我已经有了相应的代码,并且效果很好!但是一个小问题是,我希望Web服务器记住替换元素的innerHTML时的更改。因此,网络服务器会记住更改,并且不会返回其默认状态。无论是使用数据库还是使用Cookie,sessionStorage,localStorage。

document.getElementById("replace1").innerHTML = localStorage.getItem("replace1");

function loadDoc() {
  var xhttp = new XMLHttpRequest();
  xhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
     localStorage.setItem("replace1",this.responseText);   
    }
  };
  xhttp.open("GET", "https://natevanghacks.com/replacements/yoinkexecutor2.html", true);
  xhttp.send();
}

1 个答案:

答案 0 :(得分:0)

在AJAX中尝试类似的操作

localStorage.setItem("replace1",this.responseText);

身体加载后:

document.getElementById("replace1").innerHTML = localStorage.getItem("replace1");

修改

  function loadSavedData(){
    var data=localStorage.getItem("replace1");
    document.getElementById("replace1").innerHTML = data?data:'No Data found';
  }
  function deleteData(){
    localStorage.removeItem("replace1");
  }
  function loadDoc() {
    var xhttp = new XMLHttpRequest();
    xhttp.onreadystatechange = function() {
      if (this.readyState == 4 && this.status == 200) {
        localStorage.setItem("replace1",this.responseText);
        document.getElementById("replace1").innerHTML =this.responseText;
        console.log(this.responseText);
      }
    };
    xhttp.open("GET", "filename.html", true); //put your filename here...
    xhttp.send();
  }
<button onclick="loadDoc()">Send request and load Data
</button>
<button onclick="loadSavedData()">load Saved Data
</button>
<button onclick="deleteData()">Delete saved Data
</button>
<br>
<div id="replace1">
</div>

注意:您只能在http://https://协议上运行此文件