To include a shared fragment of HTML, load a trusted file and place its contents into a designated element on each page. This HTML inclusion technique works on a static server with JavaScript. Plain HTML has no universal include statement; a server template or a build tool can assemble the page before it reaches the browser instead.
Include HTML with a small static-site example
Imagine a site with two pages that need the same navigation. Keep the links in one fragment, then let each page request it. The HTML include workflow below uses one script for both pages and checks the response before inserting anything.
Create this layout in a directory named site:
site/
index.html
about.html
fragments/
nav.html
assets/
include.js
Put this in fragments/nav.html. It is a fragment, not a complete document:
<nav aria-label="Primary">
<a href="/index.html">Home</a>
<a href="/about.html">About</a>
</nav>
Put the following in index.html. Create about.html with the same contents, changing the heading to About. Both pages contain a placeholder for the navigation and refer to the same script.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Example site</title>
<script src="/assets/include.js" defer></script>
</head>
<body>
<div id="site-nav"></div>
<main><h1>Home</h1><p>Welcome to the site.</p></main>
</body>
</html>
Put this code in assets/include.js:
async function loadNavigation() {
const target = document.getElementById("site-nav");
try {
const response = await fetch("/fragments/nav.html");
if (!response.ok) throw new Error(`HTTP ${response.status}`);
target.innerHTML = await response.text();
} catch (error) {
console.error("Navigation could not be loaded:", error);
target.textContent = "Navigation is unavailable.";
}
}
loadNavigation();
From inside site, run python -m http.server 8000, then open http://localhost:8000/index.html and http://localhost:8000/about.html. Both pages should display the same two links. To load the shared markup into another page, add the same placeholder and script reference there. Change the fragment, refresh each page, and both menus should reflect the edit. This local server is for development; use a suitable web server or hosting service for deployment.
Follow the request and the paths
The deferred script runs after the document has been parsed, so the placeholder is available when loadNavigation looks it up. The browser requests the fragment separately, reads the response as text, and parses it into the placeholder. The rest of the page can appear while the request is in progress. Opening the files directly with a file:// URL can encounter browser restrictions, so serve them over HTTP for this exercise. When adding the fragment to a page, keep its expected placeholder in the document; otherwise the script has nowhere to put the result. The Fetch API documentation explains that an HTTP error response still resolves the promise, which is why the example checks response.ok.
Each path beginning with / is relative to the origin’s root. For the local server, /fragments/nav.html means http://localhost:8000/fragments/nav.html, regardless of which example page requested it. The same applies to the links inside the inserted fragment. If you deploy under a subdirectory such as /demo/, adjust the script URL, fragment URL, and navigation links to include that prefix. A root-relative path does not automatically inherit the page’s deployment folder.
By contrast, fragments/nav.html is resolved against the page’s base URL, not the script file’s folder. A <base> element can change that base. Relative links inserted from the fragment are also resolved in the containing document, not automatically relative to the fragment file. MDN’s URL guide shows how relative references are resolved. Keeping paths deliberate prevents a shared menu from working on the home page and failing on a nested page.
Decide when to assemble the page
A client-side HTML include keeps the source files simple, but the menu appears only after JavaScript runs and the request succeeds. A visitor with JavaScript disabled will see the fallback page content but no menu in this example. For navigation that must be present immediately, assemble the shared markup on the server or during a build instead.
With a server template, a template engine combines a page and a partial when serving a request. With a static-site generator, the build process combines them ahead of time and publishes complete pages. Both approaches can reuse a single maintained fragment while delivering the navigation in the initial response. This reuse of page markup leaves the source fragment editable in one place, while the generated files each contain their own copy. Their exact syntax and path rules depend on the chosen tool; configure and test that tool rather than expecting the browser to interpret its directives.
If you include HTML on the client, budget for an extra request and decide how failures should appear. A build-time HTML include avoids that browser request for the fragment, though rebuilding and redeploying is necessary after an edit. The right choice depends on whether you already have a build pipeline and whether the content must be available before scripts execute.
Keep the fragment trustworthy
You can include HTML from your own controlled files in this example, but do not insert arbitrary user-submitted markup with innerHTML. It parses the string as markup and can introduce cross-site scripting through malicious attributes or other content. MDN’s security guidance explains the risk. For plain user text, use textContent; for untrusted rich content, use an appropriate, maintained sanitizer and a security policy.
A reusable page fragment should also have stable IDs and accessible labels. If it appears more than once on one page, duplicate IDs can break references. Keep one navigation landmark here, and test its links with a keyboard after loading. If the response comes from another origin, that server must permit the request through CORS; the same-origin example avoids that configuration.
Diagnose a missing menu
When you include HTML and the menu does not appear, the most likely cause is a wrong path. Open developer tools, select the Network panel, reload the page, and find the request for nav.html. A 404 points to a missing file at the requested URL: compare that URL with your server’s document root, file names, and any deployment prefix. Fix the path, reload, and confirm a successful response contains the expected fragment. If the request is absent, check the Console for a script error and verify that the script URL and placeholder ID match the example.
