--- 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 ` 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 `