The „link“ element is a blessing. It allows for easy embedding of stylesheets and JavaScripts needed in multiple documents. It didn’t allow the embedding of HTML files, though. To achieve that we were limited to the „iframes“ element or the JavaScript method „XMLHttpRequest()“. Thanks to the new HTML5 Imports, we can now use the „“ element to load one HTML file into another.
Simple Markup in the Header
Embedding an HTML file is simple. All we need to do is use the common „<link>“ element. Then we add the value „import“ to the „rel“ attribute. Using „href“ we attach the URL of the HTML file, just like we are used to when it comes to stylesheets and scripts.
<link href="extern.html" rel="import" />
Should the file to be imported contain further „style“ or „script“ elements, these will be imported as well. It does not matter whether these elements are marked up in the head or the body of the document. One use case for HTML5 Imports could well be, to collect all the stylesheets and scripts needed in a given project and add to one single HTML file, which then gets imported into all the other project files. This largely simplifies the maintenance of bigger projects.
While the imported document contains stylesheets, these will be directly apllied. Previously defined stylesheets of the parent document will be overwritten. To make sure this doesn’t happen, take care of the position of the import link in the parent document. The same is true for JavaScripts. Even externally referenced elements from the imported document will be loaded into the parent document.
Accessing Contents of the Import File
Other content from the imported document will be loaded, but not displayed. All the texts, images and other media will not be visible as they are not part of the DOM tree of the parent document. You can still access them via JavaScript, though.
var extern = document.getElementsByTagName("link")[0].import;
Our example assumes that the first „link“ element loads an HTML file. Using the JavaScript property „import“ we write the complete tree structure of the imported file to a variable. From there we are able to access the individual nodes via JavaScript.
var absatz = extern.getElementsByTagName("p")[0];
From here we can access and read all nodes using the common JavaScript methods such as „getElementsByTagName()“. Now we are able to add them comfortably to the DOM tree of the parent document. In a more radical approach we could as well replace the „body“ element completely with the contents of the imported file.
window.addEventListener("load", function() { document.getElementsByTagName("html")[0].replaceChild(extern.getElementsByTagName("body")[0], document.getElementsByTagName("body")[0]); }, false);
Our example replaces the content of the „body“ element using „replaceChild()“. To make sure the replacement doesn’t start before all elements are loaded, the function is bound to an event listener added via „addEventListener()“.
Browser Support
At the time of this writing only Chrome supports HTML5 Imports. Also you have to enable the functionality manually. Call „chrome://flags“ from the address bar, which gives you access to the experimental features. Look for „Activate HTML Imports“ and – well – activate it. After a fresh start of your Chrome HTML5 Imports will be available.
Use the following function to check whether a given browser supports HTML5 Imports.
if ("import" in document.createElement("link")) { // HTML5 Imports are supported. }
Older browsers can make use of a polyfill, which emulates HTML5 Imports in unsupported browsers. That way there is no need to go without HTML5 Imports in modern web projects.
Pro-Tip
Sign up for a free Jotform account to create powerful online forms in minutes — with no coding required.
(dpe)
Send Comment:
7 Comments:
More than a year ago
1. Comments say this is deprecated. WHAT is deprecated? is supported across all browsers.
2. You say "add the value `import` to the `rel` attribute." But MDN doesn't show an `import` value for `rel`
More than a year ago
This feature is deprecated.
While you updated your article on 24 Feb 2022 and make it clickbait, you could have noted that it doesn't work anymore.
More than a year ago
I tried several attempts to include an html menu file in a table cell, but it does not work.
More than a year ago
I agree
More than a year ago
From the mdn site:
Obsolete since Google Chrome 73
This feature is obsolete. Although it may still work in some browsers, its use is discouraged since it could be removed at any time. Try to avoid using it.
More than a year ago
Great Article, but is it possible to import form from my j2.html and embed that form into my new page j3.html. I want to do this because my j2.html page contains dynamic adding form fields so at run time when the dynamic field is added this field should be saved in my original form and the whole form should appear on a new page by clicking on the preview form button. Please help me through this
More than a year ago
Great article and maybe a solution for a problem I had a few weeks ago with my wordpress and that was : how could I add html and Javascript animation into a wordpress post. Pages allows us to make an individual template where we can incorporate the animation, but a post not at all.
So now I wonder if this is the solution. Something that I will try out this week.