---
url: "https://web.dev/articles/imports"
titulek: "HTML Imports | Articles | web.dev"
h1: "HTML Imports Stay organized with collections Save and categorize content based on your preferences."
popis: ""
jazyk: "en"
kanonicka: "https://web.dev/articles/imports"
drobecky: ["Articles", "HTML Imports"]
zdroj: "live"
wayback_snapshot: ""
stazeno: "2026-09-14 03:31:32"
znaku: 23930
slov: 3376
kodovani: "iso-8859-2"
sha256: "6c252cd8bcf2d8ba0c11723bb3864c32707fa33c9756a78cbaa522ee03e637c6"
---
HTML Imports
Stay organized with collections
Save and categorize content based on your preferences.
Include for web
Eric Bidelman
Why imports? Think about how you load different types of resources on the web. For JS, we have `
...
...
` Let this sit. It's exciting stuff. Load/error events The `` element fires a `load` event when an import is loaded successfully
and `onerror` when the attempt fails (e.g. if the resource 404s). Imports try to load immediately. An easy way avoid headaches
is to use the `onload`/`onerror` attributes: `
` Or, if you're creating the import dynamically: `var link = document.createElement('link'); link.rel = 'import'; // link.setAttribute('async', ''); // make it async! link.href = 'file.html'; link.onload = function(e) {...}; link.onerror = function(e) {...}; document.head.appendChild(link);` Using the content Including an import on a page doesn't mean "plop the content of that file here". It means "parser, go off an fetch this document so I can use it". To actually use the content, you have to take action and write script. A critical `aha!` moment is realizing that an import is just a document. In fact, the content of an import is called an *import document*. You're able to **manipulate the guts of an import using standard DOM APIs**! link.import To access the content of an import, use the link element's `.import` property: `var content = document.querySelector('link[rel="import"]').import;` `link.import` is `null` under the following conditions: The browser doesn't support HTML Imports. The `` doesn't have `rel="import"`. The `` has not been added to the DOM. The `` has been removed from the DOM. The resource is not CORS-enabled. **Full example** Let's say `warnings.html` contains: `
Warning!
This page is under construction
Heads up!
This content may be out of date
` Importers can grab a specific portion of this document and clone it into their page: `
...
` Scripting in imports Imports are not in the main document. They're satellite to it. However, your import can still act on the main page even though the main document reigns supreme. An import can access its own DOM and/or the DOM of the page that's importing it: **Example** - import.html that adds one of its stylesheets to the main page `
...
` Notice what's going on here. The script inside the import references the imported document (`document.currentScript.ownerDocument`), and appends part of that document to the importing page (`mainDoc.head.appendChild(...)`). Pretty gnarly if you ask me. Rules of JavaScript in an import: Script in the import is executed in the context of the window that contains the importing `document`. So `window.document` refers to the main page document. This has two useful corollaries:
functions defined in an import end up on `window`. you don't have to do anything difficult like append the import's `
``index.html
` Registering custom elements [Custom Elements](/articles/customelements) is another Web Component technology that plays absurdly well with HTML Imports. Imports can execute script, so why not define + register your custom elements so users don't have to? Call itâŚ"auto-registration". `elements.html
I'm a shadow-element using Shadow DOM!
` This import defines (and registers) two elements, `` and ``. The first shows a basic custom element that registers itself inside the import. The second example shows how to implement a custom element that creates Shadow DOM from a ``, then registers itself. The best part about registering custom elements inside an HTML import is that the importer simply declares your element on their page. No wiring needed. `index.html
( I'm in the light dom )
` In my opinion, this workflow alone makes HTML Imports an ideal way to share Web Components. Managing dependencies and sub-imports Sub-imports It can be useful for one import to include another. For example, if you want to reuse or extend another component, use an import to load the other element(s). Below is a real example from [Polymer](https://polymer-project.org). It's a new tab component (``) that reuses a layout and selector component. The dependencies are managed using HTML Imports. paper-tabs.html (simplified): `
` App developers can import this new element using: `
` When a new, more awesome `` comes along in the future, you can swap out `` and start using it straight away. You won't break your users thanks to imports and web components. Dependency management We all know that loading JQuery more than once per page causes errors. Isn't this
going to be a *huge* problem for Web Components when multiple components use the same library? Not if we use HTML Imports! They can be used to manage dependencies. By wrapping libraries in an HTML Import, you automatically de-dupe resources.
The document is only parsed once. Scripts are only executed once. As an example, say you define an import, jquery.html, that loads a copy of JQuery. `jquery.html
` This import can be reused in subsequent imports like so: `import2.html