2

I am developing a website for an organization. For that, I have a menu. If I want to add a new menu item, I need to update all of the .html files to add that menu item.

Can anyone give a simple solution, so that if I add a menu item to a file, the menu should be updated in all of the files?

Danny Beckett
  • 20,529
  • 24
  • 107
  • 134
Sankar
  • 162
  • 2
  • 13

1 Answers1

3

I'd strongly recommend using PHP:

<?php include "header.html"; ?>

This works on both Linux & Windows, and both Apache & IIS.


However, if that is not an option, you can use Server Side Includes:

File in the same dir:

<!--#include file="header.html"-->

File in a different dir:

<!--#include virtual="/header.html"-->

You'll need an Apache (not IIS) server for this to work. You'll also need to use the .shtml file extension.


Alternatively, given that you want to keep .html extensions, you can make Apache "think" that all .html files are actually .php:

Create a .htaccess file at the root of your website and add this line:

AddType application/x-httpd-php .html .htm

If your are running PHP as CGI (probably not the case), you should write instead:

AddHandler application/x-httpd-php .html .htm 

(Taken from this answer)

Community
  • 1
  • 1
Danny Beckett
  • 20,529
  • 24
  • 107
  • 134
  • I am not using any PHP. Only HTML, Java Script – Sankar May 31 '13 at 11:20
  • You can have jquery file which will generate the menu for you. Just include that file on your html pages. So you only need to change the jquery file, whenever any changes is made in menu items. – Hitesh May 31 '13 at 11:21
  • I need to use only HTML and Java Script – Sankar May 31 '13 at 11:21
  • @Sankar The easiest, non-PHP, thing you can do is SHTML, as detailed in my answer about SSI's. However, you can use AJAX, i.e. `XmlHttpRequest` to load external stuff with JS. – Danny Beckett May 31 '13 at 11:22
  • `LocalStorage` could store which menu's are visible and what is in them – Isaac May 31 '13 at 11:22