\n
\n
\n\n\nHere, your header and footer are dynamically loaded across your static pages, ensuring uniformity and maintainability.","image":"https://explain.codes/media/static/images/eightify-logo.svg","author":{"@type":"Person","name":"Alex Kataev","url":"https://explain.codes//author/alex-kataev"},"publisher":{"@type":"Organization","name":"Rational Expressions, Inc","logo":{"@type":"ImageObject","url":"https://explain.codes/landing/images/[email protected]"}},"datePublished":"2024-09-10T21:45:01.313Z","dateModified":"2024-09-10T21:45:02.859Z"}
Explain Codes LogoExplain Codes Logo

Common Header / Footer with Static HTML

html
responsive-design
best-practices
seo
Alex KataevbyAlex Kataev·Sep 10, 2024
TLDR

To integrate common headers and footers, make use of JavaScript's fetch method. Generate .html elements for your header and footer, and use them within your page's specific placeholders.

<script> // The force is strong with our header... fetch('header.html') .then(response => response.text()) .then(html => document.getElementById('header').innerHTML = html); // And our footer? Obviously, it strikes back... fetch('footer.html') .then(response => response.text()) .then(html => document.getElementById('footer').innerHTML = html); </script> <div id="header"></div> <div id="footer"></div>

Here, your header and footer are dynamically loaded across your static pages, ensuring uniformity and maintainability.

Implementing Reusability: Optimal Methods

Make SSI your sidekick against redundancy

Server Side Includes (SSI) is a powerful, yet underexploited asset, especially valid for managing static content. It brings down the need for relying on third-party scripting languages, thus enhancing the simplicity and minimizing dependencies. Implementation varies across different servers like Apache, NGINX, so be sure to check your server documentation for implementation guidance.

Gain a convenient ally with jQuery

If client-side solutions appeal to you, use jQuery's .load() method - it's both efficient and easy to deploy:

// No Sith Lords were harmed in the loading of these headers and footers... $("#header").load("header.html"); $("#footer").load("footer.html");

But be wary, jQuery brings along an additional library dependency for your site to load.

Straight insertion with HTML tags

For a controlled inclusion, <object> or <embed> tags are your best shots. They might not be popular for headers and footers, but stand unbeaten for uniform content:

<!-- This is no hologram, just embedding Obi-Wan style --> <object type="text/html" data="header.html"></object> <!-- May the Footer be with you... Always --> <object type="text/html" data="footer.html"></object>

Jedi Pure JavaScript

JavaScript methods such as document.write() or modules can help you avoid framework dependencies:

// Trust me, this is the HTML you are looking for... document.write('<header>...Your Header Content...</header>');

It's handy, but watch out for side-effects like document reflows and impacts on SEO.

SEO and JavaScript: A delicate balance

While JavaScript aids in integrating common elements like headers and footers, it can mess with SEO. Search engines may not always execute JavaScript efficiently, leading to dangling content indexing issues. Use server-side or hybrid solutions to avoid this.

Embedding tags and cautious browser support

If you decide on <embed> or <object>, make sure to verify compatibility across browsers and devices. While these tags have wide modern browser support, exceptions are not unheard of.

The double-edged sword of JavaScript

Leaning heavily on JavaScript or client-side scripting comes with a price. Users with disabled JavaScript, ad blockers, or dinosaur browsers might not experience the site as desired. Implementing fallback content strategies is a good countermeasure.

Tailoring Choices to Your Needs

Each method of integrating common elements presents its own benefits and drawbacks:

  • Server-side includes are performance-friendly and SEO amicable, but require access to server configurations.
  • Client-side includes are flexible and easier to setup but affect SEO and might bring in more dependencies.
  • Direct embedding is less common but robust, given the browser supports it.

Base your choice on your technical constraints, audience preferences, and SEO requirements to align with optimal website reliability, functionality, and maintainability.