0

I have this simple html:

  <head>
      <h1>Prueba</h1>
  </head>

  <body>

      <form method="POST" action="">
            Introduce KeyWord <input type="text" name="key">
            <input type="submit" name="submit" value="Submit">
      </form>

  </body>

Now, i want to include inside other html which has severak .js attached. I have seen different ways but i only get include only the file html, without the .js attached.

Any help? Thank you so much!

Edit: This is the other html that i want include inside, with the js attached:

<html>
 <head>
  <meta charset="UTF-8">
  <title>Word Cloud</title>
  <script src="d3.js" charset="utf-8"></script>
  <script src="d3.layout.cloud.js"></script>
  <script src="d3.wordcloud.js"></script>
  <script src="example.words.js"></script>
  </head>
  <body style="text-align: center">
  <h1>Word Cloud</h1>
  <div id='wordcloud'></div>

  </body>
</html>
AntonioMJ
  • 27
  • 1
  • 4

3 Answers3

1

Demo Link : https://plnkr.co/edit/kByh82pSAw6sQAOFp5gO?p=preview

you can include html file using this javascript function:

javascript: and html code:

    function includeHTML() {
      var z, i, elmnt, file, xhttp;
      /*loop through a collection of all HTML elements:*/
      z = document.getElementsByTagName("*");
      for (i = 0; i < z.length; i++) {
        elmnt = z[i];
        /*search for elements with a certain atrribute:*/
        file = elmnt.getAttribute("w3-include-html");
        if (file) {
          /*make an HTTP request using the attribute value as the file name:*/
          xhttp = new XMLHttpRequest();
          xhttp.onreadystatechange = function() {
            if (this.readyState == 4) {
              if (this.status == 200) {elmnt.innerHTML = this.responseText;}
              if (this.status == 404) {elmnt.innerHTML = "Page not found.";}
              /*remove the attribute, and call this function once more:*/
              elmnt.removeAttribute("w3-include-html");
              includeHTML();
            }
          }      
          xhttp.open("GET", file, true);
          xhttp.send();
          /*exit the function:*/
          return;
        }
      }
    };
    includeHTML();
 <!DOCTYPE html>
 <html>
    <body>
      <div w3-include-html="<**html-name-file**>"></div> 
    </body>
 </html>

i found this code there

nitishk72
  • 1,616
  • 3
  • 12
  • 21
JacopoDT
  • 365
  • 4
  • 17
-1

You can make use of an iframe to embed another website into yours like so:

<iframe src="other.html" height="200" width="300"></iframe>

In this case other.html contained links to other pages in the website and links to js files.

kudeh
  • 883
  • 1
  • 5
  • 16
  • If he decides to use 5 or 10 different additional html files, this will fall apart really quickly. Even at 3 it would get really slow. – Max Baldwin May 10 '18 at 17:19
  • @MaxBaldwin He seems to be interested in adding only one html file. I think this is the simplest way to go about it especially if it's a small application that's not gonna get huge. – kudeh May 10 '18 at 17:55
-1

Demo Link: https://plnkr.co/edit/kByh82pSAw6sQAOFp5gO?p=preview This one is working absolutely fine.

Keep one thing in mind that if you will test on your PC it will not work directly. You will get an error in your console Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https..

So, do test on the server I mean localhost

Happy Coding

<!DOCTYPE html>
<html>
   <script>
      function includeHTML() {
        var z, i, elmnt, file, xhttp;
        /*loop through a collection of all HTML elements:*/
        z = document.getElementsByTagName("*");
        for (i = 0; i < z.length; i++) {
          elmnt = z[i];
          /*search for elements with a certain atrribute:*/
          file = elmnt.getAttribute("data-include");
          if (file) {
            /*make an HTTP request using the attribute value as the file name:*/
            xhttp = new XMLHttpRequest();
            xhttp.onreadystatechange = function() {
              if (this.readyState == 4) {
                if (this.status == 200) {
                    // Get the DOMPareser to parse String into HTML
                    var  parser = new DOMParser();
                    var  doc = parser.parseFromString(this.responseText, "text/html");;
                    elmnt.innerHTML = this.response ;
                    var allscripts = doc.getElementsByTagName('script');              
                    // Run all the Javascript
                    for(var k = 0; k<allscripts.length;k++){
                      var x = allscripts[k].innerHTML;
                      eval(x);
                    }
                  }
                if (this.status == 404) {elmnt.innerHTML = "Page not found.";}
                /*remove the attribute, and call this function once more:*/
                elmnt.removeAttribute("data-include");
                includeHTML();
              }
            }      
            xhttp.open("GET", file, true);
            xhttp.send();
            /*exit the function:*/
            return;
          }
        }
      };
   </script>
   <body>
      <div data-include="text.html"></div>
      <script>
         includeHTML();
      </script>
   </body>
</html>
nitishk72
  • 1,616
  • 3
  • 12
  • 21