The Runner The Runner - 8 months ago 58
Ajax Question

Ajax http request close

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>


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

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

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;
};"GET", "mypage.html", true);



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


<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;