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.
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.
Expanding lolo's answer, here is a little more automation if you have to include a lot of files. Use this JS code:
$(function () {
var includes = $('[data-include]')
$.each(includes, function () {
var file = 'views/' + $(this).data('include') + '.html'
$(this).load(file)
})
})
And then to include something in the html:
<div data-include="header"></div>
<div data-include="footer"></div>
Which would include the file views/header.html and views/footer.html.
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
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>
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.
I found a script by w3schools that lets you add HTML to an HTML file from an external HTML file, and I was wondering what more experienced programmers though of it. https://www.w3schools.com/w3css/w3data_includes.asp
For example, if I don't want to copypasta my header to every page on my site, I can type <header w3-include-HTML="header.html"></html> instead. The script inserts the code from header.html as a child of this header tag.
You have to include a script tag at the bottom of the body calling the w3IncludeHTML() function to get it to work. I do not know if the script works when this script tag and the external inclusion tag are not siblings of each other, or if it works when the script is not at the bottom of the body tag. However, from the testing I've done in brackets, it works wonderfully and is very easy to use.
I'm just not sure how efficient it is / if it falls under the negative side of w3school's reputation.
What do others think of this script? Are there better, simpler ways to do this, other than using stuff like iframe?
It's not that bad if it gets the job done. Generally however, if you're looking to do this sort of thing then you do it server side, because it is much faster (around 2-3x faster).
If you break down what is happening, your file is downloaded, and that html file says to download a script file, so it makes another request. then once it downloads the script, the script says to make another request to download the missing html. If you put all the html, js, and css together on the server, only one request needs to be made (it can even be cached for more speed).
So in all its completely fine to use on your own site, but is generally frowned apon for large sites.
If you want an alternative then you'll need to say which webserver you're using.
So, let's look at how w3 implemented this:
function w3IncludeHTML() {
var z, i, elmnt, file, xhttp;
z = document.getElementsByTagName("*");
for (i = 0; i < z.length; i++) {
elmnt = z[i];
file = elmnt.getAttribute("w3-include-html");
if (file) {
xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
elmnt.innerHTML = this.responseText;
elmnt.removeAttribute("w3-include-html");
w3IncludeHTML();
}
}
xhttp.open("GET", file, true);
xhttp.send();
return;
}
}
}
The first thing that jumps out at me is this:
z = document.getElementsByTagName("*");
That grabs every element on the page. OK for small sites, but if you have lots of elements, this is not great. If your tests don't show it to be inefficient, it's because it's too small of a test case.
Then it loops through every element, checking to see if it has a non-standard attribute called `w3-include-html'.
For the first one it finds, it starts an AJAX request to download the contents of the referenced file, then returns. So only the first element that has the w3-include-html will actually be set. Also note: if your server is slow to respond with the referenced file, the div that's supposed to have that content will be blank with no indication of loading or that content is coming.
But wait! In the callback for the AJAX request, once the HTML comes back, it re-runs the w3IncludeHTML function. Which means this whole process starts over again; over and over until there's no elements with that attribute left on the page.
IMO, this is a bit of a messy way to do "includes". There might be a better way to do what you're asking about.
First, are you using a "back end", or is this front-end only? Are you using PHP/Django/Node.JS/ASP.NET/anything like that? If you are, each has its own way of including partial content on a page that won't involve another ajax request to the server; it would be included on the server-side and sent back to the client as one file.
If you still want to proceed with an include mechanism like this, I'd recommend you write it yourself. You can do better than they did, and it's really just one function.
What I'd recommend if you go down this route: use data attributes. You can query nodes by their data attributes, so you don't have to pull in every node and check for it. You can also use an ID (or even just capture the HTML node itself in your closure) so you don't have to return and have your callback keep chaining these calls; you could kick off all the include calls the first time and not re-call it (unless you wanted to support nested includes).
That's what I'd do.