-1

Let's say I use app.js or a bootstrap cdm link for all my HTML pages, but I don't want to add it to every single page. How can I create an index.html that includes it so all the other HTML pages first loads the index.html head and then their own head? I use javascript for my project.

I tried to search it up, but none of the questions were the same as mine. My school project used this method, so I thought it was a good idea, but I don't know how to fix it.

3
  • 1
    That's not a good idea. If you know PHP it'd be better off to use PHP to generate the pages. Commented Aug 22, 2023 at 6:04
  • Please provide enough code so others can better understand or reproduce the problem. Commented Aug 22, 2023 at 9:18
  • Why is this tagged with CSS? Commented Nov 17 at 13:51

2 Answers 2

0

You will need to create an html file that will house the links you want to keep introducing in subsequent pages such as a head.html file for example, then inside it place the links and other content you deem necessary to be in the head, for example I can place the charset and viewport meta tags and the CDN link for Bootstrap.

<meta charset="UTF-8">

<meta name="viewport" content="width=device-width, initial-scale=1.0">

<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet"
integrity="sha384-4bw+/aepP/YC94hEpVNVgiZdgIC5+VKNBQNGCHeKRQN+PtmoHDEXuppvnDJzQIu9" crossorigin="anonymous">

Then in your subsequent pages that you want to include these links in their respective head elements, you can place a script tag within their head tags or at the end of your content within the body tags with this code:

document.addEventListener('DOMContentLoaded', async () => {
    try {
         const response = await fetch("head.html");
         const headContent = await fetch("head.html").text();
         const headElement = document.querySelector("head");

         headElement.insertAdjacentHTML("afterbegin", headContent);
    } 
    catch(error) {
         console.log("Error fetching head.html", error);
    }
});

You can also put the code within its own JavaScript file then link that file within the pages you need if you want to avoid having the code plainly written within your HTML file.

Now every page that uses this will contain the

Sign up to request clarification or add additional context in comments.

Comments

0

The best and cleanest way to avoid repeating <head> content on every page is to use server-side templates (like with Node.js, PHP, ASP.NET, or Django). This allows you to put shared scripts and styles (such as Bootstrap and app.js) in one partial file, and automatically include it in all pages when the server sends them to the browser. Browsers cannot merge <head> sections from different HTML files on their own, so using a server-side templating approach is the most professional, scalable, and widely-used method for real web projects.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.