Questions tagged [html-imports]

HTML Imports are a mechanism for including and reusing HTML documents in other HTML documents. The mechanism is natively supported in Chrome and Opera, and through a polyfill in any modern browser.

The basics

Include an import on your page by declaring a

<link rel="import">

like this:

The URL of an import is called an import location. To load content from another domain, the import location needs to be CORS-enabled:

<!-- Resources on other origins must be CORS-enabled. -->
<link rel="import" href="http://example.com/elements.html">


Feature detection and support

To detect support, check if .import exists on the element:

function supportsImports() {
  return 'import' in document.createElement('link');
}

if (supportsImports()) {
  // Good to go!
} else {
  // Use other libraries/require systems to load files.
}
92 questions
29
votes
1 answer

If HTML Imports are dead/deprecated what's the best way to import your web component (X-Tag) template?

I'm working on my first X-Tag application and on it's page it says it's meant to work with Web Component API's such as 'Custom Elements, Shadow DOM, Templates, and HTML Imports'. I've started working on my templates, but what's the best option to…
Bob
  • 411
  • 1
  • 6
  • 12
28
votes
0 answers

Is there a way to use es6 modules to import html template into javascript?

There seems to be many dated questions on this topic, I cant find one more recent (2018). Also other questions just focus on getting this to work, I am focused on the right path forward for making this work following the path of ES6 Modules with…
Joelio
  • 4,621
  • 6
  • 44
  • 80
16
votes
2 answers

How to execute a script when the custom element is upgraded

I have defined a custom element and I want to execute a script only when the custom element is upgraded to its registered type. The use case is that I must call a custom method. My main html file looks like…
Mildred
  • 463
  • 4
  • 13
14
votes
2 answers

How to package, or import Html-Templates without Html-Imports

Since Html-Imports are now deprecated in Chrome (https://www.chromestatus.com/feature/5144752345317376) and will be removed, I wonder what the alternatives are. I'm currently using Html-Imports to import Html-Templates. I see only two alternatives…
treeno
  • 2,510
  • 1
  • 20
  • 36
10
votes
1 answer

Share style across web components "of the same type"

If I understand it correctly, creating an instance of a web component can be summed up as creating a shadow root and copying the markup, e.g. from a template into it: var Template = document.querySelector('#myTemplate'); var TemplateClone =…
Michael K
  • 1,070
  • 1
  • 10
  • 25
10
votes
3 answers