Including an HTML template is similar to the way we include a stylesheet, we use the tag. But instead of using rel=stylesheet, we add the link tag with rel=import. As an example, here I will include a template named template.html
<link rel="import" id="template-file" href="template.html">
To append the content in the file, we may add the script within the body. Also, in order for this following script to work, we have to put it after the rel=import.because we should ensure that the content in rel=import has been completely loaded by the browser before the script, so that the script is able to recognize the elements, the element’s id, or the classes within that file.
To begin, we select the template file with this code.
var getImport = document.querySelector('#template-file');
Then we need get the content :
var getContent = getImport.import.querySelector('#content');
Now we can append the content within the body using the JavaScript appendChild() method.
document.body.appendChild(document.importNode(getContent, true));
The content should now appear in the main file.