When your website has multiple pages, you might find that you repeat some part of your HTML file on every page. One example of this is a menu. It&'; the same on every page, except for perhaps for the button to the current page being highlighted somehow. When you find that lots of code is repeated, it can be tempting to try to store that code in a separate file and somehow reduce it to one line in all the HTML files for your website.
The first, and in my opinion the best way, is to create a javascript function to do that work for you. It will also allow that snippet of HTML to by dynamic based on parameters that are passed to the function (or global variables that are set)<script src="functions.js?version=1"></script>
Still, we wish that there were a simpler way to do this. We wish we could do that for most of the stuff in <head>. However, that stuff we need to have set when the page first loads. We can&';t wait for a javascript function. Well, we can but it&';s better if we don&';t. Something else might get the wrong impression about our website.
You might be asking yourself what some of those other methods I mentioned might be. Well, one I used to use was a php include. You don&';t really have to know php to use it. It&';s basically one line of code. So, that&';s great. There are ways to pass parameters back and forth between vanilla javascript and php. Some of these are also not that difficult. But, php runs on the server side and most php is supposed to be on the server side. If it&';s not anything private or that needs to be secured, I think you&';re OK putting it on the client-side (in a folder with your html files).
Charles Coyier lists several methods for including HTML snippets, starting with php include at CSS-Tricks.com. I think they&';re all server-side solutions, except iframes which are just HTML. Iframes have there own constraints and might not give you what you&';re looking for.
W3 Schools has their own solution to this. Basically, you add an attribute to a div (one that&';s not a real attribute). You use that attribute to specify the name of the include file. At the end of your HTML file, you execute a javascript function that&';s probably in the <head> portion of this website file or in functions.js. This function uses XMLHttpRequest to fetch the file. For some reason I can&';t fathom, it also calls the function itself. You might be better off using the newer javascript Fetch API rather than XMLHttpRequest. I find them equally confusing. Neither is really simple or straightforward IMHO. I&';ll stick with my vanilla javascript function. It&';s much easier to understand. Still, in case your interested look here for more info.