|
A JavaScript Version of /COPY
Hey, Ted:
I
enjoyed your JavaScript tip so much that I thought I'd send you one of mine.
I've often thought HTML would benefit from something like the /COPY
directive that we RPGers enjoy so much. This would be especially useful
for building a list of links that you want to appear on multiple pages
(without the need to resort to Frames).
It is possible to emulate a copy include using JavaScript stored in an
external file. Here's a simple example.
Create a text file, such as menu.js, to contain the code to be copied:
document.write("<TABLE BGCOLOR=BEIGE><TR><TD>" +
"<A HREF=" + "Page1.htm" + ">Page 1</A>" +
"</TD></TR><TR><TD>" +
"<A HREF=" + "Page2.htm" + ">Page 2</A>" +
"</TD></TR><TR><TD>" +
"<A HREF=" + "Page3.htm" + ">Page 3</A>" +
"</TD></TR></TABLE>")
Reference the text in the <SCRIPT> tag in other HTML files:
<HTML>
<HEAD>
</HEAD>
<BODY>
<SCRIPT LANGUAGE="JavaScript" SRC="menu.js">
</SCRIPT>
</BODY>
</HTML>
-- Phil Hope
Thanks, Phil. It hadn't occurred to me to use the document
object's write method.
By the way, you probably know it, but
another way to code those anchor tags is to use the back slash (\)
as an escape character for quotation marks, like this:
document.write("<a href=\"http://www.itjungle.com\">Goto MS
--</a>")
document.write("<a href=\"http://www.google.com\">Goto google</a>")
You might also like to investigate Sever-side Includes (SSI), Phil.
They let you include files in the same way, but the copied files
can contain any HTML source, not just JavaScript.
-- Ted
|