How do you load an html file?


Learn how to include HTML snippets in HTML.


The HTML

Save the HTML you want to include in an .html file:

content.html

Google Maps

Animated Buttons

Modal Boxes

Animations

Progress Bars

Hover Dropdowns

Click Dropdowns

Responsive Tables


Include the HTML

Including HTML is done by using a w3-include-html attribute:

Example

w3-include-html="content.html">


Add the JavaScript

HTML includes are done by JavaScript.

Example

Call includeHTML() at the bottom of the page:



Include Many HTML Snippets

You can include any number of HTML snippets:

Example


Try it Yourself »


w3.js is pretty cool.

https://www.w3schools.com/lib/w3.js

and we are focus

w3-include-html

but consider the below case

- 🧾 popup.html
- 🧾 popup.js
- 🧾 include.js
- 📂 partials 
   - 📂 head
         - 🧾 bootstrap-css.html
         - 🧾 fontawesome-css.html
         - 🧾 all-css.html
   - 🧾 hello-world.html


















Hello World

Script

// include.js

const INCLUDE_TAG_NAME = `data-include-html`

/**
 * @param {Element} node
 * @param {Function} cb callback
 * */
export async function includeHTML(node, {
  cb = undefined
}) {
  const nodeArray = node === undefined ?
    document.querySelectorAll(`[${INCLUDE_TAG_NAME}]`) :
    node.querySelectorAll(`[${INCLUDE_TAG_NAME}]`)

  if (nodeArray === null) {
    return
  }

  for (const node of nodeArray) {
    const filePath = node.getAttribute(`${INCLUDE_TAG_NAME}`)
    if (filePath === undefined) {
      return
    }

    await new Promise(resolve => {
      fetch(filePath
      ).then(async response => {
          const text = await response.text()
          if (!response.ok) {
            throw Error(`${response.statusText} (${response.status}) | ${text} `)
          }
          node.innerHTML = text
          const rootPath = filePath.split("/").slice(0, -1)
          node.querySelectorAll(`[${INCLUDE_TAG_NAME}]`).forEach(elem=>{
            const relativePath = elem.getAttribute(`${INCLUDE_TAG_NAME}`) // not support ".."
            if(relativePath.startsWith('/')) { // begin with site root.
              return
            }
            elem.setAttribute(`${INCLUDE_TAG_NAME}`, [...rootPath, relativePath].join("/"))
          })
          node.removeAttribute(`${INCLUDE_TAG_NAME}`)
          await includeHTML(node, {cb})
          node.replaceWith(...node.childNodes) // https://stackoverflow.com/a/45657273/9935654
          resolve()
        }
      ).catch(err => {
        node.innerHTML = `${err.message}`
        resolve()
      })
    })
  }

  if (cb) {
    cb()
  }
}
// popup.js

import * as include from "include.js"

window.onload = async () => {
  await include.includeHTML(undefined, {})
  // ...
}

output










Hello World

How do I load an HTML file in Chrome?

Open a new tab in Chrome, then press Ctrl (Windows) or Cmd (Mac) + O. It will bring up the same Open File menu. Find your HTML file and open it..
Choose File from the Chrome ribbon menu. ... .
Navigate to your HTML file location, highlight the document and click Open..

What is a HTML file and how do I open it?

An HTML file contains Hypertext Markup Language (HTML) that formats the structure of a webpage. It is stored in a standard text format and contains tags that define the page layout and content of the webpage, including the text, tables, images, and hyperlinks displayed on the webpage.

How do I import an HTML file into my website?

How TO - Include HTML.
The HTML. Save the HTML you want to include in an .html file: content.html. ... .
Include the HTML. Including HTML is done by using a w3-include-html attribute: Example. ... .
Add the JavaScript. HTML includes are done by JavaScript. Example. ... .
Include Many HTML Snippets. You can include any number of HTML snippets:.

How do I open an HTML file on a PC?

Most computers will associate your default browser with the . html file extension. That means that normally, you don't need to find a browser to open the file—you can just double-click on it, and the computer will open it in your default web browser.