2016-10-19 6 views
0

버튼을 클릭하면 Ajax을 사용하여 html 페이지를 표시합니다.Ajax http request close

내가 필요한 것은 ajax html 페이지를 닫는 또 다른 버튼을 만드는 것입니다. 나는 abort()를 시도했으나 그것이 필요한 것이 아니며 HTTP 요청을 중단하지 않기 위해 페이지를 닫아야한다.

<!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> 
+0

'xhttp.close()' –

답변

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

하나는 겸손 처음 사용하는 경우

, 당신은로드에 추가 할 필요가 버튼에 ID를 사용하여 장착 할 수 있습니다

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

코드가 완벽하지만, 코드를 닫고 다시 열면 "정의되지 않은"단어가 표시됩니다. @mplungjan –

+0

항상 같은 페이지를 열시겠습니까? 두 번째로 아약스를하지 말고 페이지를 다시 보여주세요. – mplungjan