6

So I have a website and I'm using a basic menu located at the top right corner of the screen. It's links are as follows:

| Home | Blog | Results | Pictures | Our Location |

Form time to time I need to add a new link to the menu or change where one of the links points to. This means that on every page that the menu exist I must manually change the link. Surely there is some way to have a master menu that is just put on every page.

Or am I dreaming?

Zack The Human
  • 8,373
  • 7
  • 39
  • 60
Bob Dylan
  • 4,393
  • 9
  • 40
  • 58
  • Solution available here: http://stackoverflow.com/questions/39447411/how-to-load-nav-menu-from-an-external-file-no-wamp-all-code-must-be-browser – Gabriel Sep 12 '16 at 17:09

6 Answers6

3

Put the menu in a separate file and include it on the server side, either using a full-blown scripting language like PHP (one line) or using SSI.

Max Shawabkeh
  • 37,799
  • 10
  • 82
  • 91
3

There are various ways to do this. It depends what you have access to on the server. Possibly the simplest mechanism is server-side includes. You would just have a file that contains the menu and include it on every page.

You can also do this with every programming language in more or less elegant ways.

EDIT: The SSI is quite simple. You can just make a /header.html file, then do:

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

in the appropriate place.

Matthew Flaschen
  • 278,309
  • 50
  • 514
  • 539
3

use the PHP include on all your pages

<?php include 'includes/menu.php'; ?>

and have a separate menu.php within a folder named 'includes'. you'll need to save all oyur pages as .php

you can make your footer as an include too

pixeltocode
  • 5,312
  • 11
  • 52
  • 69
2

I think this is the easiest way of doing it without a server side language. Use a javascript library like W3Data. All you need to do is add the w3-include-html attribute to your div. Done!

If your menu is in a file called menu.html you'd do something like this.

<div w3-include-html="menu.html"></div> 
Ahmed Mansour
  • 527
  • 5
  • 13
  • I have experimented with various approaches and this works perfectly. The library is w3.js from W3 schools and it's use is well documented on the W3 schools site. – Jeremy Young Oct 17 '17 at 15:41
0

Have a server side program determine what menus and locations of the links need to be there. Then use ajax to periodically poll the server side script to get the contents of the menu at the current time.

It'd be easy to have it send an xml data back like:

<menu>
    <link name="Home" href="destinationhome.html"/>
...
    <link name="Pictures" href="....html"/>
 </menu>

Then build your links from that data.

H. Green
  • 735
  • 5
  • 10
0

You can generate pages from templates before uploading them: See Template Toolkit.

Sinan Ünür
  • 116,958
  • 15
  • 196
  • 339