Forum Moderators: open

Message Too Old, No Replies

How to call code from 1 place instead of repeating in every html page

without using frames

         

d1n1_81

6:41 pm on Mar 28, 2005 (gmt 0)

10+ Year Member




Hi there,

I have a website made up of 12 pages. Each of them has the same layout with the same menu design etc.

Ideally I would like to have the code for the menu etc. in one place which I will call on from all the other pages. This means I will be able to edit the menu, add/remove/alter items etc. in one place instead of having to alter every page in turn.

However, I want to do this without using frames because I don't really like using them in page design.

Can this be done using javascript or some other tools?

Any help is greatly appreciated.

Thanks
D1n1_81

kaled

8:18 pm on Mar 28, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This is a job for server-side includes.

You typically create an html fragment (no <html>, <head> or <body> tags) that contains the common code. Let's call it common.htm

Next, you must arrange for it to be included. This requires an instruction located in a comment. The server must parse the whole file and when it locates the instruction, it inserts the html fragment. This must be enabled on your server. Typically, this is done by using the file extension .shtml but it can be enabled using the .htaccess file (I think) so that can keep your existing file names.

e.g.
<!--#include virtual="/common.htm" -->

Kaled.

kaled

10:14 pm on Mar 28, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Correction

Use virtual="url" where url is relative to the base directory.
Use file="url" where url is relative to the directory of the current document.

Kaled.

SpaceFrog

11:58 am on Mar 29, 2005 (gmt 0)

10+ Year Member



juste an external script ...

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

Saltminer

10:14 pm on Mar 29, 2005 (gmt 0)

10+ Year Member



An .htaccess file is one way to do it. The drawback being that it will parse every html file for "includes" before serving it. That could be overkill in some situations.

If your webhost is running Apache another way is to use Apache's Xbit Hack directive. All my web host providers have it set, you can try it and see if it works on your site.

Xbit allows you to retain the same html file name, you just set the execute bit on the file using chmod. Any html file that has the execute bit set the server will parse it for includes. That way it only spends time on the files that actually have an "include" in the script.

In case I lost you, as an example:
Add an "include" to your index.html file.
Upload the file to the server.
Chmod the file to "744", or "chmod +x index.html"

If the Xbit Hack is working the next time you request index.html you should get it with the include inserted into the script.

If you're on a Windows server it won't work, as you may have guessed.

kaled

10:40 pm on Mar 29, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



That's a very interesting bodge. I've not heard of that - I think I may see if my host supports it.

Kaled.

d1n1_81

9:01 pm on Mar 30, 2005 (gmt 0)

10+ Year Member



Thanks for the help everyone.

I went for the javascript option which seems to be working fine at the moment.

Cheers

kaled

10:08 pm on Mar 30, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



With javascript, bear in mind

1) Some users don't have it enabled.
2) Search engines will ignore any output that is normally generated using document.write() etc.

These are general comments - not restricted to your situation.

Kaled.