81

Is there a decent way with static HTML/XHTML to create common header/footer files to be displayed on each page of a site? I know you can obviously do this with PHP or server side directives, but is there any way of doing this with absolutely no dependencies on the server stitching everything together for you?

Edit: All very good answers and was what I expected. HTML is static, period. No real way to change that without something running server side or client side. I've found that Server Side Includes seem to be my best option as they are very simple and don't require scripting.

Adam Haile
  • 29,081
  • 56
  • 180
  • 272
  • Which web server? IIS has a mechanism for this built in to the server. – NotMe Jan 06 '09 at 23:14
  • 2
    I was about to ask this when I found this answer through Google. – Igor Zevaka Jun 05 '10 at 13:35
  • An alternative for small websites is to use just **one HTML page** (with a single header and footer) and **toggle sections with Javascript** or just use **anchors** (e.g. in the header) to navigate inside the page. – collimarco Mar 05 '17 at 18:53
  • i think the answer here is fairly good. https://stackoverflow.com/questions/18712338/make-header-and-footer-files-to-be-included-in-multiple-html-pages# – Joe Sep 13 '17 at 17:48

14 Answers14

36

There are three ways to do what you want

Server Script

This includes something like php, asp, jsp.... But you said no to that

Server Side Includes

Your server is serving up the pages so why not take advantage of the built in server side includes? Each server has its own way to do this, take advantage of it.

Client Side Include

This solutions has you calling back to the server after page has already been loaded on the client.

35

JQuery load() function can use for including common header and footer. Code should be like

<script>
    $("#header").load("header.html");
    $("#footer").load("footer.html");
</script>

You can find demo here

phpsmashcode
  • 796
  • 6
  • 16
  • 1
    2014 and I still think this is a viable answer. The linked article provides a nice and simple demo, too. – HPWD Mar 26 '14 at 04:59
25

Since HTML does not have an "include" directive, I can think only of three workarounds

  1. Frames
  2. Javascript
  3. CSS

A little comment on each of the methods.

Frames can be either standard frames or iFrames. Either way, you will have to specify a fixed height for them, so this might not be the solution you are looking for.

Javascript is a pretty broad subject and there probably exist many ways how one might use it to achieve the desired effect. Off the top of my head however I can think of two ways:

  1. Full-blown AJAX request, which requests the header/footer and then places them in the right place of the page;
  2. <script type="text/javascript" src="header.js"> which has something like this in it: document.write('My header goes here');

Doing it via CSS would be really an abuse. CSS has the content property which allows you to insert some HTML content, although it's not really intended to be used like this. Also I'm not sure about browser support for this construct.

moffeltje
  • 4,095
  • 4
  • 24
  • 49
Vilx-
  • 97,629
  • 82
  • 259
  • 398
8

You can do it with javascript, and I don't think it needs to be that fancy.

If you have a header.js file and a footer.js.

Then the contents of header.js could be something like

document.write("<div class='header'>header content</div> etc...")

Remember to escape any nested quote characters in the string you are writing. You could then call that from your static templates with

<script type="text/javascript" src="header.js"></script>

and similarly for the footer.js.

Note: I am not recommending this solution - it's a hack and has a number of drawbacks (poor for SEO and usability just for starters) - but it does meet the requirements of the questioner.

DanSingerman
  • 34,397
  • 12
  • 76
  • 92
5

The best solution is using a static site generator which has templating/includes support. I use Hammer for Mac, it is great. There's also Guard, a ruby gem that monitors file changes, compile sass, concatenate any files and probably does includes.

juanpazos
  • 109
  • 1
  • 2
  • 8
5

you can do this easily using jquery. no need of php for such a simple task. just include this once in your webpage.

$(function(){
    $("[data-load]").each(function(){
        $(this).load($(this).data("load"), function(){
        });
    });
})

now use data-load on any element to call its contents from external html file you just have to add line to your html code where you want the content to be placed.

example

<nav data-load="sidepanel.html"></nav>
<nav data-load="footer.html"></nav>
Shubham Badal
  • 985
  • 11
  • 32
  • 1
    I like this method, too. Do you know how SEO this would be (not a big issue, just curious)? – HPWD Aug 17 '14 at 22:46
  • i am not sure but i think yes, it will affect seo. robots will read source of html page but as you can see when you view-source you will not find html content loaded. the html is embedded only when client executed javascript. simple parsing through html will not find sidepanel and footer etc content – Shubham Badal Aug 18 '14 at 09:14
5

The simplest way to do that is using plain HTML.

You can use one of these ways:

<embed type="text/html" src="header.html">

or:

<object name="foo" type="text/html" data="header.html"></object>
BSMP
  • 3,862
  • 8
  • 31
  • 41
Jose
  • 51
  • 1
  • 1
3

The most practical way is to use Server Side Include. It's very easy to implement and saves tons of work when you have more than a couple pages.

allesklar
  • 8,977
  • 6
  • 33
  • 50
2

You could use a task runner such as gulp or grunt.

There is an NPM gulp package that does file including on the fly and compiles the result into an output HTML file. You can even pass values through to your partials.

https://www.npmjs.com/package/gulp-file-include

<!DOCTYPE html>
<html>
  <body>
  @@include('./header.html')
  @@include('./main.html')
  </body>
</html>

an example of a gulp task:

var fileinclude = require('gulp-file-include'),
    gulp = require('gulp');

    gulp.task('html', function() {
        return gulp.src(['./src/html/views/*.html'])
            .pipe(fileInclude({
                prefix: '@@',
                basepath: 'src/html'
            }))
            .pipe(gulp.dest('./build'));
    });
Antfish
  • 849
  • 15
  • 34
2

HTML frames, but it is not an ideal solution. You would essentially be accessing 3 separate HTML pages at once.

Your other option is to use AJAX I think.

Joe Phillips
  • 44,686
  • 25
  • 93
  • 148
2

You can try loading them via the client-side, like this:

<!DOCTYPE html>
<html>
  <head>
    <!-- ... -->
  </head>
  <body>

    <div id="headerID"> <!-- your header --> </div>
    <div id="pageID"> <!-- your header --> </div>
    <div id="footerID"> <!-- your header --> </div>

    <script>
      $("#headerID").load("header.html");
      $("#pageID").load("page.html");
      $("#footerID").load("footer.html");
    </script>
  </body>
</html>

NOTE: the content will load from top to bottom and replace the content of the container you load it into.

LogicalBranch
  • 3,582
  • 2
  • 16
  • 48
1

No. Static HTML files don't change. You could potentially do this with some fancy Javascript AJAXy solution but that would be bad.

Gareth
  • 115,773
  • 31
  • 143
  • 154
1

Short of using a local templating system like many hundreds now exist in every scripting language or even using your homebrewed one with sed or m4 and sending the result over to your server, no, you'd need at least SSI.

Keltia
  • 13,953
  • 3
  • 27
  • 30
0

The only way to include another file with just static HTML is an iframe. I wouldn't consider it a very good solution for headers and footers. If your server doesn't support PHP or SSI for some bizarre reason, you could use PHP and preprocess it locally before upload. I would consider that a better solution than iframes.

Chuck
  • 222,660
  • 29
  • 289
  • 383