HTML5 Import provides a standard way of importing html from a separate file onto the page, in the similar way we reference JavaScript and CSS. This can also be used to bundle JS, CSS and HTML. Without bundling you will have to have style and script referenced on all your pages, and also in a particular order. This way you can bundle everything for your custom element so that consumer don't have to worry about referencing necessary CSS/JS. HTML from external file must be added to the head of the page.
<link rel="import" href="myimport.html" id="myimport">
JavaScript and CSS from the imported file get executed and applied immediately where as html is inert, meaning it's loaded in the browser but is not rendered. So if you have console.log('i am coming from myimport.html') javascript in the myimport.html, this will get executed by just putting above link. But to render html from the imported page you need to do following
<script>
var content = document.getElementById('myimport').import;
var markupimportedfromfile = content.getElementById('fromimportedfile');
document.body.appendChild(markupimportedfromfile);
</script>
With the template tag, everything inside (markup or JS or CSS) is inert until cloned and this will still be the case if it is in the import file, but any CSS and JS directly defined in the import file will be executed\applied immediately. CORS rule will be applied for importing html.
Imported file can also have import within it. You can use this for importing JS/CSS library, this way sub-import file will have these libraries and you will import this file in all your custom element file. This can help in avoiding different custom element using different version of those libraries.
Referencing Owner Document
If you use selector on document object (document.querySelector) in the import file, it will search through the DOM defined in the file where it is imported. To get reference from the import file, you must use selector on document.currentScript.ownerDocument
Events
The link element has two events, onload and onerror. Script for that need to be added before adding the link
<script>
function loaded(event) {
console.log('import loaded');
}
function error(event) {
console.log('could not load ' + event.target.href);
}
</script>
<link rel="import" id="myimport" href="myimport.html" onload="loaded(event)" onerror="error(event)">`