gunawantw

include HMTL

Nov 14th, 2019
426
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 1.18 KB | None | 0 0
  1. <!--
  2. https://www.w3schools.com/howto/howto_html_include.asp
  3. -->
  4.  
  5. <!DOCTYPE html>
  6. <html>
  7. <script>
  8. function includeHTML() {
  9.   var z, i, elmnt, file, xhttp;
  10.   /*loop through a collection of all HTML elements:*/
  11.   z = document.getElementsByTagName("*");
  12.   for (i = 0; i < z.length; i++) {
  13.    elmnt = z[i];
  14.    /*search for elements with a certain atrribute:*/
  15.    file = elmnt.getAttribute("w3-include-html");
  16.    if (file) {
  17.      /*make an HTTP request using the attribute value as the file name:*/
  18.      xhttp = new XMLHttpRequest();
  19.      xhttp.onreadystatechange = function() {
  20.        if (this.readyState == 4) {
  21.          if (this.status == 200) {elmnt.innerHTML = this.responseText;}
  22.          if (this.status == 404) {elmnt.innerHTML = "Page not found.";}
  23.          /*remove the attribute, and call this function once more:*/
  24.          elmnt.removeAttribute("w3-include-html");
  25.          includeHTML();
  26.        }
  27.      }      
  28.      xhttp.open("GET", file, true);
  29.      xhttp.send();
  30.      /*exit the function:*/
  31.      return;
  32.    }
  33.  }
  34. };
  35. </script>
  36.  
  37. <body>
  38.  
  39. <div w3-include-html="content.html"></div>
  40.  
  41. <script>
  42. includeHTML();
  43. </script>
  44.  
  45. </body>
  46. </html>
Add Comment
Please, Sign In to add comment