In my opinion the best solution uses jQuery:

a.html:

<html> 
  <head> 
    <script src="jquery.js"></script> 
    <script> 
    $(function(){
      $("#includedContent").load("b.html"); 
    });
    </script> 
  </head> 

  <body> 
     <div id="includedContent"></div>
  </body> 
</html>

b.html:

<p>This is my include file</p>

This method is a simple and clean solution to my problem.

The jQuery .load() documentation is here.

Answer from lolo on Stack Overflow
🌐
W3Schools
w3schools.com › howto › howto_html_include.asp
How To Include HTML
Including HTML is done by using a w3-include-html attribute:
🌐
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():
Discussions

Include another HTML file in a HTML file - javascript
The w3-include-HTML methode is not really efficient. The script which they have wrote as an example is quite awkward and complicated. This is much more efficient. More on stackoverflow.com
🌐 stackoverflow.com
HTML5 - Why does my w3-include-html tag not work?
Because that's not a normal html tag. It only works if you include w3school's Javascript file. More on reddit.com
🌐 r/learnprogramming
3
0
November 1, 2016
Can you link to an HTML file? - Stack Overflow
W3schools has an include. They also have there own CSS as a side note. More on stackoverflow.com
🌐 stackoverflow.com
HTML - w3-include-html reading javascript issue - Stack Overflow
I am using codepen.io to create a project and in that project, I have created two html pages. The first is called "header.html" and the second is "index.html". I am using "w3-include-html" to inclu... More on stackoverflow.com
🌐 stackoverflow.com
🌐
GitHub
gist.github.com › h5ng › 8b072af9c730591328b91c6f2d98b371
w3-include-html · GitHub
w3-include-html. GitHub Gist: instantly share code, notes, and snippets.
🌐
Reddit
reddit.com › r/learnprogramming › html5 - why does my w3-include-html tag not work?
r/learnprogramming on Reddit: HTML5 - Why does my w3-include-html tag not work?
November 1, 2016 -

I want to include html code (a header) in another html page (index page).

Using php (include, echo) does work, but why does the tag w3-include-html not work :/.

I used it like this:

<div w3-include-html="path/name/info/htmlcode"></div>

Reference: http://www.w3schools.com/w3css/w3data_includes.asp

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.

Find elsewhere
🌐
GitHub
github.com › validator › validator › issues › 625
[Suggestion] w3-include-html attribute recognition · Issue #625 · validator/validator
March 28, 2018 - It occurred in my code at a line with: <div w3-include-html="../nav.html"></div>. There could be something that says Attribute "w3-include-html" should be replaced by using an "<iframe>" instead of "div".
Author   scoutchorton
🌐
Experts Exchange
experts-exchange.com › questions › 29048402 › how-to-insert-html-into-html-file.html
Solved: how to insert html into html file | Experts Exchange
August 4, 2017 - <!DOCTYPE html> <html> <script src="https://www.w3schools.com/lib/w3.js"></script> <body> <div w3-include-html="content.html"></div> <script> w3.includeHTML(); </script> </body> </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():
🌐
Stack Overflow
stackoverflow.com › questions › 75392783 › using-w3-include-html-breaks-some-tags-and-js-scripts-stop-working
Using w3-include-html breaks some tags and JS scripts stop working
<!DOCTYPE html> <html lang="en-US"> <head> <!-- <link rel="icon" type="image/png" href="/icons/terminal16x16.png" sizes="16x16"> <link rel="icon" type="image/png" href="/icons/terminal48x48.png" sizes="48x48"> --> <div w3-include-html="modules/header.html"></div> </head> <body> <!-- ...
🌐
Quanzhanketang
quanzhanketang.com › howto › howto_html_include.html
How To Include HTML - W3Schools Online Web Tutorials
Well organized and easy to understand Web bulding tutorials with lots of examples of how to use HTML, CSS, JavaScript, SQL, PHP, and XML.
🌐
Webdevable
webdevable.com › w3schools › howto › howto_html_include.html
How To Include HTML - w3schools - WEBDEVABLE
Tabs Dropdowns Accordions Convert Weights Animated Buttons Side Navigation Top Navigation JS Animations Modal Boxes Progress Bars Parallax Login Form HTML Includes Google Maps Loaders Tooltips Slideshow Filter List Sort List · HTML, CSS, JavaScript, PHP, jQuery, Bootstrap and XML. Read More » ... Your message has been sent to W3Schools.
🌐
W3C
w3.org › TR › WD-html40-970708 › struct › includes.html
Inclusions in HTML documents
For instance, the following line will include the contents of piece_to_include.html at the location where the OBJECT definition occurs.
🌐
TutorialsPoint
tutorialspoint.com › how-to-include-another-html-file-in-an-html-file
How to include another HTML file in an HTML file?
Includes a wrapper DOM with the attribute w3-include-html having the new HTML file name as the value.
🌐
Sololearn
sololearn.com › en › Discuss › 3233556 › how-can-i-inherit-the-contents-of-one-html-file-into-another
How can I inherit the contents of one html file into another? | Sololearn: Learn to code for FREE!
August 16, 2023 - <!DOCTYPE html> <html> <head> <title>Page Title</title> </head> <body> <div w3-include-html="first.html"></div> <div w3-include-html="second.html"></div> </body> </html> 17th Aug 2023, 7:25 PM · JaScript · + 5 · Евгений , We simply have different opinions.