Keeping header and footer in separate file

4 replies · opened Sep 15, 2022

Jjonasku888Sep 15, 2022

Hello
I am using the index-corporate as my template.
I am trying to keep the header and the footer in a separate html file so I can then include it into the different pages so I only need to change the one header/footer file to update all pages.

I tried to use

  1. php includes: <?php include "header.html" ?>
  2. with JS
    putting <nav class=navbar> in html file and having JS file of
    const nav = document.querySelector('.navbar')
    fetch('header.html')
    .then(res=>res.text())
    .then(data=>{
    nav.innerHTML=data
    const parser = new DOMParser()
    const doc = parser.parseFromString(data, 'text/html')
    eval(doc.querySelector('script').textContent)
    })

However, some styles get lost even after copying the whole header section from style.css into the header.html file. and the slider below the menu does not get appear.

How should I proceed? To be able to keep headers and footers in a separate file to make it modular?

Thanks a lot!
Kind regards
Jonas

Jjonasku888Sep 15, 2022

So like this the Slider below is not working, the search bar is not working and the transition from transparent header to normal header is not working. Arrows for the submenu also dissapear.

If I only keep the primary navigation in a separate file then the style of it is lost. but everything else would work more or less.

Jjonasku888Sep 15, 2022

I found an old post on here and was able to overcome it with my 1. method by further changing file type to .php. Is there any downside using .php instead of .html or things that I have to be aware of?

Kind regards
Jonas

Jjonasku888Sep 15, 2022

.

SSemicolon WebSTAFFSep 16, 2022

Hello,

Thanks for your patience!

The First option: php includes: `` will work fine, however, you are using this incorrectly.

  1. You will need to change the extension of the File from .html to .php. There are no downsides to this.

  2. header.html should be header.php.

That's it. This will definitely work fine. Hope this Helps!

Let us know if we can help you with anything else or if you find any further issues.

Have the same question, or something new?

Sign in to the Canvas dashboard to reply or open your own topic. Canvas owners get direct help from the SemiColonWeb team.

Reply on the dashboard