Log in

View Full Version : Referencing an external file



daddioja
07-27-2006, 07:50 PM
I am working on a website that has many pages. Each page has the same header with drop down menus on the top. I want each page to get the source code for the headers from an external file. Part of the problem is that I want some of the code to import into <HEAD> and some of the code must drop into <BODY>. The content is contiguous, so it can post as one item across the head and the top of the body, but I am not sure how to make it happen. Any help? Thanks.

boogyman
07-27-2006, 08:38 PM
what you are asking is possible, but I would need you to give me the code or a url to get the code otherwise I will not know where to break it up

daddioja
07-27-2006, 10:08 PM
Sure pissa, the page is at www.ctap4.org. The drop down menus are the problem. I have many people updating pages, but it becomes difficult to update the menus. I want the information on the menues and the header graphics separate. Actually, the menu information itself is in the <head>, so i could have only that code reference an external file. Thanks for any help you can provide. Jason

superjadex12
07-27-2006, 10:44 PM
if you are using php you can use : include ('external_filename.inc');
you can include any amount of files you wish, so you can have for example:

<head>
include ('header_part.inc');
</head>

<body>
include ('body_part.inc');

....
Or you can just use one for entire top portion , just being sure to have the <body> tag in there where you want the breaks.

You can google "php includes" for more help, but I sure hope you are using php because this is an extremely easy and effecient solution.

daddioja
07-27-2006, 11:30 PM
Thanks, but we aren't using php. I was hoping for another solution, but if there are none, I guess we will need to change. Any other ideas?

superjadex12
07-28-2006, 10:11 AM
what are you using then? Just general html?

I would say that if you aren't using any (other) server side script, then your best bet IS php. The reason being that you can run your entire page in plain html and have the only instances of php be your includes.

Forgive me if I simplify my solution, as I am not calling you a novice or anything just want to be clear if , indeed, you don't know anything about php.

So basically if you're paying for your hosting, chances are its supports php. Thats basically all you need. A php supported server. Then you would map out your pages EXACTLY as you would in html. Then, locate the place where you want your external pages to load and place this code :


<?php
include ('name_of_external_file.inc');
?>
(note your filename could be anything, header.html, header.php etc. it is just common to use ".inc" which represents "include")

The <?php ?> tags open and close your php code. Everything that is not contained between php tags gets interpreted as html. So basically you can have a full html site, with only two snippets of php to load your pages.

Then all you need to do is rename any pages containing your php includes to
page_name.php

And that's it. Again, please excuse me if you know all this, as I am in no way an advanced coder. The thing is I lay out all my pages in this exact way because yes, having external headers and footers saves A TON of useless hours hand coding potentially hundreds of pages!

Let me know if this helps.

jr_yeo
07-28-2006, 11:38 AM
<script src="header.js">
</script>
<script src="footer.js">
</script>


superjadex12's idea is good too if daddioja is using any server side scripts :p

if ur website plain old HTML this ur best bet :D

i think :p

daddioja
07-28-2006, 06:52 PM
We are running cgi scripts and have naming requirements for the pages. If I understand correctly, php pages need to end in the .php extension, but I am not an expert so I easily could be dead wrong.

In either case, I was hoping to run a javascript as mentioned by jr yeo. However, I am having trouble. Is it because part of the content is a java script? Here is the script I am trying to contain in the external file. Thanks for the help so far. Anything else will surely earn you good karma points.

<script language="JavaScript">
<!--
function fwLoadMenus() {
if (window.fw_menu_0) return;
window.fw_menu_0 = new Menu("root",140,23,"Arial, Helvetica, sans-serif",12,"#ffffff","#000000","#7490c2","#99afd1");
fw_menu_0.addMenuItem("Mission Statement","location='about_us/mission.htm'");
fw_menu_0.addMenuItem("Description","location='about_us/description.htm'");
fw_menu_0.addMenuItem("Plan","location='about_us/plan.htm'");
fw_menu_0.addMenuItem("Brochure","location='about_us/brochure.pdf'");
fw_menu_0.addMenuItem("State Map","location='about_us/statemap.htm'");
fw_menu_0.addMenuItem("Regional Map","location='about_us/regionalmap.htm'");
fw_menu_0.addMenuItem("Staff","location='about_us/staff.htm'");
fw_menu_0.addMenuItem("Council","location='about_us/council.htm'");
fw_menu_0.hideOnMouseOut=true;
fw_menu_0.writeMenus();
} // fwLoadMenus()
//-->
</script>

Twey
07-28-2006, 06:54 PM
if ur website plain old HTML this ur best bet :pActually, using Javascript for includes is pretty daft.

If you really can't use a server-side language to do it, I suggest you add it to all the pages individually. The *n?x utility sed might help you to do so and to update it later.

If you put that directly into a .js file, it won't work. You have HTML tags (<script></script>) and HTML comments (<!-- -->) in there. Remove them.

What sort of CGI scripts are you running? It is likely that you could use those to perform the insert.

superjadex12
07-28-2006, 07:53 PM
yeah, if you have to stick with cgi (maybe perl? ) then that's your best bet, I would guess to google perl includes, cgi includes, etc as I am not familiar with that.

I would really emphasize NOT using javascript, because your entire website would then depend on a code that can be (easily) be disabled or just plain not supported. Accessability basically goes down the drain... whereas serverside is always executed, regardless of browser...

p.s. yeah for php all files containing php have to end in .php

jr_yeo
07-28-2006, 08:01 PM
m not good at any server side scripting either :cool: its still ur choice no matter how daft and lame we think using javascript for includes really are :p

daddioja
07-28-2006, 11:01 PM
Thanks for the help everyone. Your advice pointed me in the perfect direction. I did get it sorted out.

Since the part I wanted external was a javascript for drop down menus, I ended up using this script to call the external javascript menu file.

<SCRIPT language="JavaScript" SRC="http://www.ctap4.org/menu.js"></SCRIPT>

superjadex, can you explain your thoughts here a little more.


I would really emphasize NOT using javascript, because your entire website would then depend on a code that can be (easily) be disabled or just plain not supported. Accessability basically goes down the drain... whereas serverside is always executed, regardless of browser...


The javascript has been working in all browsers I have tested on multiple platforms, but is there something else I should be aware of? Thanks again.

Twey
07-28-2006, 11:42 PM
The javascript has been working in all browsers I have tested on multiple platforms, but is there something else I should be aware of? Thanks again.You evidently haven't testing in Lynx. :)

Also, many users deliberately disable Javascript for security reasons. There was a (nother) remote exploit that gave full access to the attacker to a Windows system a little while back. Microsoft typically took an entire week to patch it, but in the meantime it even recommended that users disable Javascript.

superjadex12
07-29-2006, 08:31 AM
daddioja,

Basically what I am getting at is that although javascript is very useful and enhances greatly the interactivity of a website, the general consensus is that
a website should not depend entirely upon js. And in your case, your navigation is entirely dependant on js. Meaning that if js was disabed (and as Twey pointed out, its not so rare as you may think) , or not supported etc, then your site will cease to perform.

I want to tell you I am no professional, I have no formal training , I don't get paid, etc. So I am getting all my facts from the guys that care to share their knowledge here on the internet. And time and again, I read that all well-done, progressive coding is based primarily on accessability. The idea that you want your code to be as compliant and as accessible to as many people as possible. This means different browsers, platforms, OS, mobile devices, handicapped accessability, etc. Not only do you benefit from working with a bigger market but search engines love to work with "good", clean, functional, code.

So my opinion is , yes, It is HARDER, to code to such standards...but you benefit from it much more.

To give you an example, I am working on a site that has ajax enabled page loads. Basically it uses javascript to load content right on the current page without reloading the page. Instant content, right there! Now this sounds great But turn off javascript and what do you get? A whole lotta blank space. So to fix this, I use php as a backup. The link tags that load the javascipt, also carry links to the exact same content only in a separate page. So clicking on the link with js enabled, gives the uses instant content, but with js disabled, the standard link will load the page that holds the same content.

So in your case I think your best bet is working with your server side script. Server side script ALWAYS works! Don't you love that. If you can tell us exactly what language you're using I'm sure the guys here can help. I'm near positive that pretty much any and all server-side languages have include capability, and I'm also sure its a piece of cake too (i.e for php, include (filename.php); DONE!!!!

daddioja
07-29-2006, 06:23 PM
Thanks for the feedback guys. I will look into it a bit more. I appreciate your thoughts. I now have some homework to do.