This W3 Schools script is not a standard and upon looking at its source code, it relies on XHLHttpRequest objects (AJAX). AJAX requests will only work when the code is requested from a server over HTTP or HTTPS, so make sure when you test this code that you are running it on a server.

In addition, even if you get this working, the included file should not be an entire HTML document. It should just be a fragment that you intend to insert into a larger file. Since you are attempting to inject your header.html file into a div of index.html, you should only inject code that would be valid inside of a div, so change header.html to this:

<p id ="userName">username</p>
<p id ="time"></p>
<p id ="logcount">timeCounter</p>   
<!-- You want the .js reference to come before your
     attempt to use the code in the file and also
     after any elements that the code will refer to.  -->
<script src="scripts/userInfo.js"></script>
<script>dateTime();</script>
Answer from Scott Marcus on Stack Overflow
๐ŸŒ
W3Schools
w3schools.com โ€บ w3js โ€บ w3js_html_include.asp
W3.JS HTML Includes
HTML includes are done by JavaScript. Make sure your page has w3.js loaded and call w3.includeHTML():
๐ŸŒ
W3Schools
w3schools.com โ€บ howto โ€บ howto_html_include.asp
How To Include HTML
Including HTML is done by using a w3-include-html attribute: ... HTML includes are done by JavaScript.
๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ questions โ€บ 75392783 โ€บ using-w3-include-html-breaks-some-tags-and-js-scripts-stop-working
javascript - Using w3-include-html breaks some tags and JS scripts stop working - Stack Overflow
--> </body> <footer> <div w3-include-html="modules/footer.html"></div> <!-- <div class="container"> <small>Matthew Green &copy; 2022 - <script>document.write(new Date().getFullYear())</script> </small> </div> --> </footer> <script src="scripts/include_html.js"></script> <script>includeHTML();</script> <script src="//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.13.1/highlight.min.js"></script> <script async defer src="https://buttons.github.io/buttons.js"></script> <script> document.addEventListener("DOMContentLoaded", function () { document.querySelectorAll("pre code").forEach(block => { window.hljs.highlightBlock(block); }); }); </script> </html>
๐ŸŒ
Webdevable
webdevable.com โ€บ w3schools โ€บ w3js โ€บ w3js_include.html
W3.JS Includes - WEBDEVABLE
HTML includes are done by JavaScript. Make sure your page has w3.js loaded and call w3.includeHTML():
Find elsewhere
๐ŸŒ
W3schools-fa
w3schools-fa.ir โ€บ w3js โ€บ w3js_html_include.html
W3.JS Includes
HTML includes are done by JavaScript. Make sure your page has w3.js loaded and call w3.includeHTML():
๐ŸŒ
CSS-Tricks
css-tricks.com โ€บ the-simplest-ways-to-handle-html-includes
The Simplest Ways to Handle HTML Includes | CSS-Tricks
January 26, 2021 - Wish this was a thing in html. Been using @hammerformac for a while for this. ... Another cool method is using web components. Define a custom element i.e. <my-header> write the template code and include the javascript.
๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ questions โ€บ 51544410 โ€บ can-you-use-w3-include-html-with-js
javascript - Can you use w3-include-html with JS? - Stack Overflow
July 26, 2018 - 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.";} /*rem
Top answer
1 of 4
9

Simple way would be to put the header part in a separate html file.

Now load this file in html code using jQuery load function like

$("#headerDiv").load("header.html")

Know that, this will require web server because load function sends a request to server.

Check out the code sample:

demo.html

<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
<script> 
   $(function(){
    $("#headerDiv").load("header.html");
   });  
</script>
</head>
<body>
<div id="headerDiv"></div>
<!-- Rest of the code -->
</body>
</html>

header.html

<div >
    <a>something</a>
    <a>something</a>        
</div>
2 of 4
5

That is called HTML includes, and YES, it is possible

<div w3-include-HTML="content.html">My HTML include will go here.</div>
<script>
(function () {
  myHTMLInclude();
  function myHTMLInclude() {
    var z, i, a, file, xhttp;
    z = document.getElementsByTagName("*");
    for (i = 0; i < z.length; i++) {
      if (z[i].getAttribute("w3-include-html")) {
        a = z[i].cloneNode(false);
        file = z[i].getAttribute("w3-include-html");
        var xhttp = new XMLHttpRequest();
        xhttp.onreadystatechange = function() {
          if (xhttp.readyState == 4 && xhttp.status == 200) {
            a.removeAttribute("w3-include-html");
            a.innerHTML = xhttp.responseText;
            z[i].parentNode.replaceChild(a, z[i]);
            myHTMLInclude();
          }
        }      
        xhttp.open("GET", file, true);
        xhttp.send();
        return;
      }
    }
  }
})();
</script>

NOTES

HTML doesn't have a simple include mechanism (except for frames like iframe, which have side effects).

A better solution would be to use Server-Side includes, which is the preferred way of adding common parts to your document, on the server, of course.

๐ŸŒ
W3Schools
w3schools.invisionzone.com โ€บ general
W3.JS and using w3-include-html - General - W3Schools Forum
August 25, 2017 - I use w3-include-html to bring in certain portions of a website. I noticed on our footer page that has javascript within the html snippet does not work. Is there a way to get it to work using this library? Is there a work around? Thanks in advance!
๐ŸŒ
Simmons University
web.simmons.edu โ€บ ~grabiner โ€บ comm244 โ€บ weeknine โ€บ including-javascript.html
Including JavaScript In Your Page
The <script> tag is what we use to includes our JavaScript. It's a lot like the <link> tag you've already been using to include your CSS files. Here's a very basic snippet of JavaScript using the script tag. This JavaScript is written directly into our HTML page.
๐ŸŒ
Mzntech
mzntech.com โ€บ edu โ€บ w3js โ€บ w3js_html_include.asp
W3.JS Includes
HTML includes are done by JavaScript. Make sure your page has w3.js loaded and call w3.includeHTML():