loading html from external files

(skip to setup)

so i want to break down my home page into smaller chunks of html while still displaying it all on the same page, and i'm going to use javascript to do it.

im sure lots or people have done this before and many will do it again, but this is my version. you know, make what you need and all that.

why?

other ways to do it

so what im going to do is:

considerations

setup

the container

the target is just a div containing a hyperlink:

<div class="loadexternal">
<a href="/clubs/kitty.html">­kitties</a>
</div>

the link points to the page you want to load.

the external content

the content needs to be placed inside the <main></main> tags of a stand-alone html page. anything else on the page will be ignored. for an example of one of these files, see /clubs/kitty.html.

the javascript

loadexternalstuff.js
// runs asynchronously when page ready
document.addEventListener­("DOMContentLoaded", (event) => {
  async function loadExternalStuff()
  {
    try 
    {
      // find all the divs with class="loadexternal"
      const selector = ".loadexternal";
      const divs = document­.querySelectorAll(selector);
    
      for(const div of divs)
      {
        // find a hyperlink inside the div
        const a = div­.querySelector("a");
        if(a)
        {
          // load html from the url
          const url = a.href.toString();
          const response = await window.fetch(url);
          if(!response.ok)
          {
             console.log(response.status);
             
            // if the html failed to load, skip forward to the next div
             continue;
          }
          
          // convert the html to a dom object 
          const raw = await response.text();
          const dom = new DOMParser()
            .parseFromString(raw, "text/html");

          // grab the contents of <main> and ignore everything else
          const mainHtm = dom.querySelector("main")
            .innerHTML;

          // replace everything in the div with the new code
          div.innerHTML = mainHtm;
         }
      }    	    
    } 
    catch (error) 
    {   
      console.dir( "Error loading XML:" + error );
      console.dir(error);
    }
  }
  loadExternalStuff();
});

download it here

(you shouldn't need to change the js code to use it, but feel free to do so.)

how it works

the code:

demo

here are some cute kitty pixels loaded in from my master page at /clubs/kitty.html

Kitty Friends Pixel Club

if it worked you should see some pretty kitties. if it didn't work (eg. javascript is switched off) you should still see a plain link to the kitty master page!

notice the meta data, header, and footer were discarded after loading, i only capture the <main> element.

conclusion

well, it works on my machine, so i guess that's it! by the time i had finished writing these notes i'd already deployed the code in various places around the site. and it's already made things more maintainable!

what's it doing?

on the home page, unsorted links are loaded from unsorted.html and the button wall is loaded from neighbours.html

in the dog+house, kitty pixels are loaded from kitty.html