Javascript Includes?

They have: 4 posts

Joined: Nov 2000

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!

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's picture

He has: 4,048 posts

Joined: Aug 2000

Hey Mag, Welcome to TWF!

They have: 4 posts

Joined: Nov 2000

Shocked

Thanks for pointing out that rather simple mistake to me Hugh Smiling 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's picture

They have: 634 posts

Joined: Dec 1999

Hi MagB & as Max said: welcome to the forum that likes to spin webs round the masters Smiling

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

They have: 4 posts

Joined: Nov 2000

Thanks fore that great answer Vincent Smiling 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.