Skip to content

JS library that allows HTML includes, and dispatches an event after completion

License

Notifications You must be signed in to change notification settings

Hybridx24/includeme

Repository files navigation

includeme.js

includeme.js is a simple JS library that allows the use of <includeme></includeme> element tags to include separate html files into a page.

The advantage of using includeme.js over other libraries is that it comes with an includemeCompleted event listener, which allows JS to be run after all of the external html files have been merged into the DOM. This is especially important if you have JS that needs to target included HTML files.

Installation

  1. (Optional) Install polyfills for Promise, fetch and forEach to increase compatibility with older browsers:

<script crossorigin="anonymous" src="https://polyfill.io/v3/polyfill.min.js?features=Promise%2Cfetch%2CNodeList.prototype.forEach"></script>

  1. Download includeme.min.js located in the dist/ folder, and include it in your site before your closing body tags.

How to Use

An example would be putting this at the bottom of your <body></body> tags.

<script crossorigin="anonymous" src="https://polyfill.io/v3/polyfill.min.js?features=Promise%2Cfetch%2CNodeList.prototype.forEach"></script>
<script type="text/javascript" src="path/to/includeme.min.js"></script>

<script>
    document.addEventListener('includemeCompleted', function (e) {
        console.log('All external HTML files have been loaded!')
    })
</script>

and then you can your html files from anywhere in the body as follows:

<includeme src="path/to/file.html"></includeme>

Security

Since this library uses element.outerHTML, it is important that this library is only used to include html files created by the websites developers. Including external or user submitted files can lead to XSS attacks.

Compatibility (w/ Polyfills)

  • Chrome 15+
  • Edge 15+
  • Safari 7+
  • IE 9+
  • FF 20+
  • Opera 35+

About

JS library that allows HTML includes, and dispatches an event after completion

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published