【问题标题】:How can I prevent the content from being reloaded when they click Back on the browser?当他们在浏览器上单击“返回”时,如何防止内容被重新加载?
【发布时间】:2019-02-21 22:18:38
【问题描述】:

我正在使用带有 JavaScript 的 JSON API 并在 HTML 中添加该内容并添加该内容,但是当您转到另一个页面并在浏览器中单击返回时,内容会重新加载,所以我想知道一种避免这种情况的方法,这是我正在使用的 JavaScript 代码。

var request = new XMLHttpRequest();
request.open('GET', 'https://localhost:3000/api', true);
request.onload = function () {
  var data = JSON.parse(this.response);
  if (request.status >= 200 && request.status < 400) {
    data.forEach(v => {
      const card = '<span><div class="card">v.content</div></span>';
      $("#data").append(card);
    });
  }else{
    console.log('Error al cargar el contenido');
  }
}
request.send();

感谢您的帮助

【问题讨论】:

标签: javascript html json api


【解决方案1】:

您必须在本地保存来自 get 请求的响应并从中加载。

查看类似问题的答案https://stackoverflow.com/a/17104536/10997917

【讨论】:

    猜你喜欢
    • 2012-06-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多