hurriedly% 2016-10-19 12:00 采纳率: 100%
浏览 103

Ajax http请求关闭

I am using Ajax to display an html page when a button is clicked.

What I need is to create another button to close the ajax html page. I have tried the abort() but that's not what I need, I need to close the page not to abort the HTTP Request.

<!DOCTYPE html>
<html>

<body>


  <p id="demo">When you click the button it will load an HTML page</p>

  <button type="button" onclick="loadDoc()">Open HTML Page</button>

  <script>
    function loadDoc() {
      var xhttp;
      if (window.XMLHttpRequest) {
        // code for modern browsers
        xhttp = new XMLHttpRequest();
      } else {
        // code for IE6, IE5
        xhttp = new ActiveXObject("Microsoft.XMLHTTP");
      }
      xhttp.onreadystatechange = function() {
        if (this.readyState == 4 && this.status == 200) {
          document.getElementById("demo").innerHTML = this.responseText;
        }
      };
      xhttp.open("GET", "mypage.html", true);
      xhttp.send();
    }
  </script>

</body>

</html>
  • 写回答

1条回答 默认 最新

  • weixin_33730836 2016-10-19 12:03
    关注
    <button type="button" 
      onclick="document.querySelector('#demo').style.display='none'">Close HTML Page</button>
    

    or

    <button type="button" 
      onclick="document.querySelector('#demo').innerHTML==''">Close HTML Page</button>
    

    Either can be attached unobtrusively using an ID on the button

    If you use the first, you need to add to loading:

    document.querySelector("#demo").innerHTML = this.responseText;      
    document.querySelector('#demo').style.display="";
    
    评论

报告相同问题?

悬赏问题

  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口