npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

load-html

v1.2.0

Published

include HTML code inside HTML pages using a custom tag `load-html` to load content dynamically

Downloads

35

Readme

load-html

include HTML code inside HTML pages using a custom tag load-html to load content dynamically

Features | Usage | API | Annotated source | License

Features

  • Load HTML snippets from remote URLs, recursively.
  • Can be used to load Web Components, as an alternative to HTML imports: see Web Components Template example.
  • Supports IE 10.
  • Since it uses innerHTML it will not execute script tags.
  • Can be used on modern browsers to load WebComponents, see WebComponents example folder

Usage

See usage example folder or read below.

Start with your index.html

<!doctype html>
<html>
  <head>
    <title>load-html usage example</title>
  </head>
  <body>
    <load-html src="helloWorld.html">Loading...</load-html>
  </body>
</html>

Content inside <load-html> custom HTML tag is optional.

Create files helloWorld.html and linkToHomepage.html in the same folder.

<!-- helloWorld.html -->

<h1>Hello World</h1>

<load-html src="linkToHomepage.html"></load-html>
<!-- linkToHomepage.html -->

<p>
  This content was loaded by <a href="https://g14n.info/load-html">load-html</a>.
</p>

Import loadHtml function some how, for example, add the following tag to your index.html:

<script src="https://unpkg.com/load-html"></script>

Then invoke it on window load, for instance add the following snippet to your index.html:

<script>
  window.addEventListener('load', function () {
    loadHtml();
  })
</script>

API

loadHtml(callback)

You can pass an optional callback function as argument:

  • It will be executed when <load-html /> nodes are loaded.
  • Loaded nodes will be passed as first argument.
  • Note that loading is recursive, hence callback function could be executed more than once.
<script>
  window.addEventListener('load', function () {
    loadHtml(function (nodes) {
      console.log('load-html nodes loaded: ' + nodes.length)
    });
  })
</script>

NOTA BENE The nodes argument passed to callback is a NodeList.

Although NodeList is not an Array, it is possible to iterate over it with forEach()

For example, using something like nodes.filter(node => !node.getAttribute('error')) will fail.

However you may want to filter those nodes that did not loaded correctly. Do something like

<script>
  window.addEventListener('load', function () {
    loadHtml(function (nodes) {
      nodes.forEach(node => {
        if (node.getAttribute('error')) {
          return
        }

        // Do something with your node...
      })
    });
  })
</script>

Annotated source

Start with attribution comment: web site and license.

// https://g14n.info/load-html
// License: MIT

Just define a global loadHtml function.

function loadHtml (callback) {

Select all <load-html /> tags. Note the loaded attribute is used to achieve recursive loading.

  var nodes = document.querySelectorAll('load-html:not([loaded])');
  var toBeLoaded = nodes.length;

Fetch the HTML content for each node.

  nodes.forEach(function (node) {
    try {
      var loader = new XMLHttpRequest();
      loader.addEventListener('load', function loadHtml () {
        if (loader.status == 200) {
          node.innerHTML = loader.responseText;
        }

Keep track of number of DOM nodes loaded, then try to repeat recursively. When done, invoke callback, if any.

        node.setAttribute('data-loaded', true);
        toBeLoaded--;

        if (toBeLoaded == 0) {
          if (typeof callback == 'function') {
            callback(nodes)
          }

          loadHtml(callback);
        }
      });

Send request to fetch content.

      loader.open('GET', node.getAttribute('src'), true);
      loader.send();

Store error, mark node as loaded.

    } catch (error) {
      console.error(error);
      node.setAttribute('data-error', error.message);
      node.setAttribute('data-loaded', true);
    }
  })
}

Export it as a global function.

window.loadHtml = loadHtml;

License

MIT