개발 알다가도 모르겠네요

html안에서 외부 html 불러오기 본문

웹/Javascript

html안에서 외부 html 불러오기

이재빵 2022. 8. 25. 01:25
728x90
<!-- html파일 불러오기 -->
    <div data-include-path="navbar.html"></div>
    <script>
      window.addEventListener("load", function () {
        var allElements = document.getElementsByTagName("*");
        Array.prototype.forEach.call(allElements, function (el) {
          var includePath = el.dataset.includePath;
          if (includePath) {
            var xhttp = new XMLHttpRequest();
            xhttp.onreadystatechange = function () {
              if (this.readyState == 4 && this.status == 200) {
                el.outerHTML = this.responseText;
              }
            };
            xhttp.open("GET", includePath, true);
            xhttp.send();
          }
        });
      });
    </script>