Giter Club home page Giter Club logo

htmlimports's Introduction

Learn the tech

Why HTML Imports?

This repository contains a Javascript polyfill for the HTML Imports specification.

HTML Imports are a way to include and reuse HTML documents in other HTML documents. As <script> tags let authors include external Javascript in their pages, imports let authors load full HTML resources. In particular, imports let authors include Custom Element definitions from external URLs.

Basic usage

For HTML imports use the import relation on a standard <link> tag, for example:

<link rel="import" href="import-file.html">

Polyfill details

Getting Started

Include the html-imports.js or html-imports.min.js (minified) file in your project.

<script src="HTMLImports/html-imports.js"></script>

html-imports.js is the debug loader and uses document.write to load additional modules. Use the minified version (html-imports.min.js) if you need to load the file dynamically.

Polyfill Notes

In imported documents, href and src attributes in HTML, and url properties in CSS files, are relative to the location of the imported document, not the main document.

The HTML Imports polyfill begins processing link tags when the DOMContentLoaded event fires. To know when loading is complete, listen for the HTMLImportsLoaded event on document or window.

Example:

<script>
window.addEventListener('HTMLImportsLoaded', function(e) {
  // all imports loaded
});
</script>

The polyfill loads linked stylesheets, external scripts, and nested HTML imports, but does not parse any data in the loaded resources. For parsing imports, combine HTML Imports with Custom Elements. As long as the HTML Imports is loaded first, the Custom Elements polyfill will detect it, and process all imports when HTMLImportsLoaded event fires.

The WebComponentsReady event

Under native imports, <script> tags in the main document block the loading of imports. This is to insure the imports have loaded and any registered elements in them have been upgrade. This native behavior is difficult to polyfill so we {{site.project_title}} doesn't try. Instead the WebComponentsReady event is a stand in for this behavior:

<script>
  window.addEventListener('WebComponentsReady', function(e) {
    // imports are loaded and elements have been registered
  });
</script>

Other notes

  • In a native HTML Imports, document.currentScript.ownerDocument references the import document itself. In the polyfill use document._currentScript.ownerDocument (note the underscore).

Tools & Testing

For running tests or building minified files, consult the tooling information.

htmlimports's People

Contributors

dfreedm avatar ebidel avatar frankiefu avatar morethanreal avatar pennyfx avatar robdodson avatar secretrobotron avatar sigmundch avatar sorvell avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.