Skip to content

Until there are proper HTML Includes, you can do it the wrong way. People who complain about how many calls this makes can never install tracking scripts on a website.

License

Notifications You must be signed in to change notification settings

lnsy-dev/html-partial

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

html-partial

Until there are proper HTML Includes you can do it the wrong way. This component will fetch the targeted file and inject it as a string into the divs inner HTML.

You shouldn't use this in production.

Usage

Include the html-partial.js file, then you can use it like so:

<html-partial src="html-url"></html-partial>

or:

<html-partial src="html-url" shadowdom></html-partial>

This partial will then fetch the targeted file and inject it as a string into the divs inner HTML.

This is the code:

class HTMLPartial extends HTMLElement {

 connectedCallback(){
   this.shadowdom = this.getAttribute('shadowdom')
   this.src = this.getAttribute('src')
   if(this.src === null){
      this.innerHTML = '<error>HTML PARTIAL REQUIRES SOURCE</error>'
   }
   fetch(this.src)
    .then(res => res.text())
    .then(res => {
      if(this.shadowdom){
        this.shadow = this.attachShadow({mode: 'open'})
        this.shadow.innerHTML = res
      } else {
        this.innerHTML = res
      }
    })
  }
}

customElements.define('html-partial', HTMLPartial)

About

Until there are proper HTML Includes, you can do it the wrong way. People who complain about how many calls this makes can never install tracking scripts on a website.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published