Server-Side Includes, CSS and JavaScript files...
If you link many documents to a single file, it makes sense that a single change within that file is instantly brought to the ones linked to it.
Let's start from scratch...
Plain old default.html file...
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"><head><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /><meta name="Keywords" content="" /><meta name="Description" content="" /><title>Easy Maintenance</title><style type="text/css" media="screen, projection"><!--img {border:0;}.myclass {color:red;background-color:yellow;font-size:120%;}--></style><script type="text/javascript"><!--// triggers an annoying alert box when the page loadsfunction diva_sayHello(){alert("Hello World!");}//--></script></head><body onload="diva_sayHello()"><div id="header"><ul><li><a href="default.html">Home</a></li><li><a href="aboutus.html">About Us</a></li>...</ul><!-- /header --></div><div id="main"><p class="myclass">The content here is specific to this document...</p><!-- /main--></div><div id="footer"><p>This is the content for the bottom of evey single page in the site...</p><!-- /footer --></div></body></html>
The content that is common to many documents in the site is highlighted below
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"><head><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /><meta name="Keywords" content="" /><meta name="Description" content="" /><title>Easy Maintenance</title><style type="text/css" media="screen, projection"><!--img {border:0;}.myclass {color:red;background-color:yellow;font-size:120%;}--></style><script type="text/javascript"><!--// triggers an annoying alert box when the page loadsfunction diva_sayHello(){alert("Hello World!");}//--></script></head><body onload="diva_sayHello()"><div id="header"><ul><li> <a href="default.html">Home</a></li><li><a href="aboutus.html">About Us</a></li>...</ul></div><!-- /header --><div id="main"><p class="myclass">The content here is specific to this document...</p><!-- /main--></div><div id="footer"><p>This is the content for the bottom of evey single page in the site...</p><!-- /footer --></div></body></html>
Note that I didn't really highlight everything that was common to the files across the site. I limited myself to the "obvious", but there is no real limit to how much a file can be "stripped".
The "Tools"
To import these 4 different sections into our document, we'll use 4 different files. We need to create:
- One Style Sheet
- One JavaScript file
- Two Includes.
Files' content
File extensions are - in theory - not really important to make things work, but it is good practice to use ".css" for CSS files, ".js" for JavaScript files and ".inc" for Includes.
- mystyle.css
-
img {border:0;}.myclass {color:red;background-color:yellow;font-size:120%;}
- myjavascript.js
-
- // triggers an annoying alert box when the page loads
function diva_sayHello(){alert("Hello World!");}
- myinclude_header.inc
-
<ul><li><a href="default.html">Home</a></li><li><a href="aboutus.html">About Us</a></li>...</ul>
- myinclude_footer.php
-
<p>This is the content for the bottom of evey single page in the site...</p>
The call to the function is still attached to the ONLOAD attribute of the BODY element. You need to leave it there. You may include a function call inside the external file if it doesn't rely on an event handler to be "fired" (for example functions declared and called inside the head tags of a document).
- If the information contained in your includes is sensitive, it is then better to use an extension relevant to the server model used to protect its content (asp, php, cf, etc.).
- Note that there is no need to keep the opening and closing comments in the external files.
Some browsers won't recognize a CSS file as containing a Styles Sheet unless it actually ends in ".css". This is because some web servers won't hand over such CSS files unless the correct MIME type is sent to the client. If you are not on an Apache web server, you'll have to ask your WHC to take care of that for you, if you are on an Apache web server, you can either edit "mime.types" and add the MIME type you want or use an ".htaccess" file. The latter would include:
AddType text/css .myOwnExtension
Linking the Styles Sheet to the Documents
This file may be "imported" in different ways:
- Using the
LINKelement: -
<link rel="stylesheet" href="mystyle.css" type="text/css" />
- Using the @import directive:
-
<style type="text/css"><!--@import url("mystyle.css");--></style>
- Using a
SCRIPTelement: -
<script type="text/javascript"><!--document.write('<link rel="stylesheet" href="mystyle.css" type="text/css" />')//--></script>
- Using a Conditional Comment:
-
<!--[if IE 5]><style type="text/css"><!--@import url("mystyle.css");--></style><![endif]-->
Only Internet Explorer 5 (Windows) will import the above Styles Sheet
These techniques allow a designer/developer to serve a different Styles Sheets depending on the visitor's browser; this is called "branching". For more on this, read "Branching Techniques".
Linking the JavaScript file to the Documents
<script type="text/css" src="myjavascript.js"></script>
Remember the previous note about the call to the function that must stay inside the document.
Linking the Includes files to the Documents
- This is for the content of the "header"
DIV: -
<!--#include virtual="/myinclude_header.inc" -->
- This is for the content of the "footer"
DIV: -
<!--#include virtual="/myinclude_footer.php" -->
By default, the server does not parse a document with an .htm or .html extension. To make sure the server "processes" these files inside your pages, give your documents an .shtml extension or an extension relevant to the server model used (check with your Web Hosting Company if you're unsure what extension to use).
The above example is proper syntax for ASP, .NET and Apache SSI. Below are examples for other Server Languages:
- ColdFusion:
-
<cfinclude template="include_footer.php">
- JSP:
-
<%@ include file="myinclude_footer.php" %>
- PHP:
-
<?php include('myinclude_footer.php'); ?>
The INCLUDE directive accepts another keyword: FILE. Use FILE to indicate a relative path and VIRTUAL to declare a root relative path. You should not use the FILE directive to link your pages to includes that sit in higher directories.
Default.html becomes default.shtml
New "plugs" are highlighted
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"><head><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /><meta name="Keywords" content="" /><meta name="Description" content="" /><title>Easy Maintenance</title><link rel="stylesheet" href="mystyle.css" type="text/css" /><script type="text/css" src="myjavascript.js"></script></head><body onload="diva_sayHello()"><div id="header"><!--#include virtual="/myinclude_header.inc" --><!-- /header --></div><div id="main"><p class="myclass">The content here is specific to this document...</p><!-- /main--></div><div id="footer"><!--#include virtual="/myinclude_footer.php" --><!-- /footer --></div></body></html>
Doing this, we've killed two birds at once; managing common data across multiple pages is a snap and reading the structure of a document is much easier.
What's next?
Using external files is supposed to save a lot of headaches. Unfortunately, reading a document that includes a lot of them can be "tricky". If the content of a JavaScript file can help one to understand what it does, most often, the name of the file itself doesn't shed much light. The same goes for includes...
So let's pick another tool in the box: the HTML comment! That's an easy fix to prevent "meaningless" lines of code:
...<title>Easy Maintenance</title><!-- Basic Style Sheet --><link rel="stylesheet" href="mystyle.css" type="text/css" /><!-- Hello World! Alert Box --><script type="text/css" src="myjavascript.js"></script></head><body onload="diva_sayHello()"><div id="header"><!-- navigation links: unordered list --><!--#include virtual="/myinclude_header.inc" --><!-- /header --></div><div id="main"><p class="myclass">The content here is specific to this document...</p><!-- /main--></div><div id="footer"><!-- links in #footer --><!--#include virtual="/myinclude_footer.php" --><!-- /footer --></div></body></html>
Your file will not validate if you have 2 hyphens( "--") within an HTML comment (within the string between the opening and closing tags, which utilize those double-hyphens).
Important things to keep in mind
- Be aware that most of the time, browsers don't return any error if they cannot load an external CSS or JavaScript file.
- You should favor absolute paths in Styles Sheets for NN4 since this browser has its own way of interpreting relative paths.
Unobtrusive Scripts and Dreamweaver Extensions