Mozilla’s not supporting them, but now that I’ve used it, I’m think that this syntax was well thought out and will be easily shimmed. (I know you hate shims, but this is really small and should package nicely.)
First, note that this isn’t going to work serving it locally right from your file system. In other words, you can’t just open your HTML files in the browser from the file explorer. If you don’t know how to set up a server, then this might be a bit too advanced for you anyway. No offense, I still suck at that stuff. (why I work as a front ender!)
Anyway, this short video consists of four parts. 1) Delivering the files from the server. 2) Linking the HTML Import. 3) Using the import. 4) Understanding styles.
Delivering your files in production won’t be a problem, but locally, you’ll need to use a web server. I use node in the video, but running a LAMP stack should be just fine. If you’re just reading from the file system, the OS doesn’t know what to do with the request and the browser blocks it anyway for security.
Linking the HTML and Using DOM API import method
This is covered in the HTML5Rocks Tutorial on imports, I change up my usage here a little bit, but between my sample code and their sample code, here’s what’s going on.
The browser starts rendering the html, see that there’s a link to load an import. The syntax for this is
<link rel="import" href="YOURFILEHERE.html">
The browser then sends a request to the server for the file. You can address this on the server side however you like (see part one).
The DOM API’s import tell the browser what you want to do with it. You can read the document just like you would read the default document.
var link = document.querySelector('link[rel="import"]');
var content = link.import;
var el = content.getElementById('inside_template');
I have this inline at the end of the page, so it waits for everything else to be loaded. In real life, you’ll want to have it wait for the resource to be ready before using it, and the above tutorial suggests verifying the feature is in place first.
Notice, we clone one of the nodes in the imported document, this is important for what you’ll see in the next. If we just copy text or try to set them equal, you’ll get different behaviors.
Styles in the Imports
If you watch the video, that behavior might seem mighty funky. Notice when cloning the imported node:
- If there’s no CSS is in the imported file, the primary file’s CSS takes over.
- CSS that specifically targets the imported node, that carries over. This is a good way to understand how import and clone works, because the node can be viewed like the object within an object that it is from the console. A simple F12 on an element opens up all the computed attributes of the node/object in the DOM.
- CSS from the import acts just like a linked style sheet. This blew my mind at first. Then… well, I realized that I was linking a file, and the browser would treat it just like any other linked file. The stuff in the head is stored somewhere, a linked stylesheet or a linked import with a style tag would effect the body the same way.