How do I append HTML from a document loaded with AJAX?

前端 未结 6 1529
闹比i
闹比i 2021-01-13 19:13

I am trying to append the contents of a .html file to the body of my main page. Basically, I am trying to make a reusable chunk of html that I can load into any page with a

6条回答
  •  梦谈多话
    2021-01-13 19:56

    you need a reference to DOM to know where to innest your loaded page. in your case you could think about appending it to body like this:

    function importHTML(url_) {
      var request = new XMLHttpRequest();
    
      request.addEventListener("load", function(event_) {
    
        document.body.innerHTML += this.responseText
    
      }, false);
    
      xmlhttprequest.open("POST", url_, true);
      xmlhttprequest.send(null);
    }
    

提交回复
热议问题