Javascript Includes?
Hi, im new here and to javascript.
My site is hosted at a free web space provider (spaceports.com) and they don´t support SSI so i was thinking about using something i read in a mail a wihile ago, javascript includes.
For example, if you wannted this simpel link to appear on evry page:
Click here to visit Site.
they sad that you shold create a .js file (menu.js) with the following contents:
here to visit
Site');
-->
And place this in the body to call the script:
<script LANGUAGE="javascript" TYPE="text/javascript"
SRC="menu.js"></script>
But i cant get it´t to work can sombody help me out?
Thanks
Magnus
HighSpeed - Wrooooom!
Hugh Jass posted this at 00:56 — 23rd November 2000.
They have: 12 posts
Joined: Oct 2000
Hi Mag,
put document.write('Click here to visit
Site');
all on one line, it should work fine then.
Regards,
Hugh Jass
Mark Hensler posted this at 01:24 — 23rd November 2000.
He has: 4,048 posts
Joined: Aug 2000
Hey Mag, Welcome to TWF!
MagB posted this at 15:56 — 23rd November 2000.
They have: 4 posts
Joined: Nov 2000
Thanks for pointing out that rather simple mistake to me Hugh I got another question will this work in all java enabled browsers, even 3.0 ones? and is there any other way you could do this on?
HighSpeed - Wrooooom!
Vincent Puglia posted this at 01:35 — 25th November 2000.
They have: 634 posts
Joined: Dec 1999
Hi MagB & as Max said: welcome to the forum that likes to spin webs round the masters
Re your question: js files should work for 3.0 browsers since the 'include' statement is really an HTML thingy, not a javascript one.
Re your original post & js files in general:
1) the file cannot contain any HTML. This includes HTML comment tags "" If you want to comment the file (and you should if it's long) you must use c-like comments:
a) "//" for one-line comments like:
alert(msg); //msg is passed from blahblah
b) "/* comment */" for multiple-line comments, like:
/* this file contains five functions
including one for the menu */
2) if you want to include HTML in your js file, it must be embedded within a javascript function or variable, for example:
a) document.write("");
b) var header = "My Title";
Note the single quotes within the doubles in example 2a.
The script tag should be placed within the head tags, as follows:
<script LANGUAGE="javascript" SRC="menu.js"></script>
<script language='javascript'>
</script>
Note: in the above example, if you have a var named 'myTitle' in the js file, it will be overwritten by the one in the normal script tags.
You can then use the information in the js file as you would if it were written within the HTML page.
Re your specific problem:
In your js file, have the following:
function myLink()
{
document.write('Click here to visit Site.');
}
in your html pages, wherever you want the link to appear, write:
....text and stuff in the body......
<script language='javascript'>
</script>
....more text and stuff in the body......
As Hugh said, the document.write needs to be on one line. I placed in a function so that you can refer to it in the html page.
If you are a javascript newbie, I recommend:
1) you see the javascript primer at: http://htmlgoodies.earthweb.com
2) buy a book for beginners (like SAM's Javascript in 24 Hours)
Vinny
Where the world once stood
the blades of grass cut me still
MagB posted this at 17:42 — 26th November 2000.
They have: 4 posts
Joined: Nov 2000
Thanks fore that great answer Vincent it was really informative, i think im going to buy me a book to learn some more.
Thanks again
Magnus
Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.