kibibyte
kibibyte

Reputation: 3037

Is it possible to create a web site header without copying and pasting it on every page?

I'm building a small-scale website (a personal one) in which each page would have the same set of header elements (I'm not talking about the <head> element). In other words, I want each page to have essentially the same title at the top of the page and the same navigation bar below that (with possibly minor differences in each page). It's kind of like how StackOverflow has that navigation bar (with the logo, and the Questions, Tags, etc. buttons) on the top of every page.

Is it possible create such a header for every web page without copying and pasting the HTML code to do so? I really don't want to run into a situation where if I want to make a single change, I would have to change all of my pages containing the header.

Upvotes: 1

Views: 2187

Answers (5)

Jonathan
Jonathan

Reputation: 1

If you have a server with PHP capabilities

include 'header.inc.php';

you must put the header code in a file named that and then put that include code in all pages that you want the header to show up on

Upvotes: -1

OverZealous
OverZealous

Reputation: 39570

You need to use some kind of dynamic page processing, whether it's PHP, a server-side include, or a similar tool.

If you need to stick with straight HTML, you could try to rig something up with AJAX or JavaScript - but then you highly limiting your website's functionality, giving it serious performance issues, AND preventing users who have JavaScript disabled from using your website.

A third answer is to use some sort of pre-deployment tool. This used to be a bigger market, but I think it's mostly dried up now. Here's an example for using DreamWeaver to handle this.

Upvotes: 1

Jeremy
Jeremy

Reputation: 4975

If you have a PHP server that supports PHP,

<?php include 'header_inc.php'; ?>

If that's not available

<!--#include virtual="header_inc.html" -->

But whether this works or not is server dependent

Upvotes: -1

C. K. Young
C. K. Young

Reputation: 223083

Real web sites use real web frameworks, which have a concept called a "layout" (at least that's what they're called in Rails; as mentioned in Uwe's answer, they're called master pages in ASP.NET). All the common "templatey" stuff goes into a layout.

Upvotes: 4

Uwe Keim
Uwe Keim

Reputation: 40736

How about include files in a server-side language like PHP or master pages in ASP.NET?

Upvotes: 2

Related Questions