Forum Moderators: open

Message Too Old, No Replies

Javascript "include," is it possible?

         

Acternaweb

9:00 pm on Jan 5, 2004 (gmt 0)

10+ Year Member



I have a script that I would like to use on many pages and rather then have the code posted on every page I want to have an include. Is it possible? How do I go about coding the page and determing the include?

I tried <script type="text/javascript" language="javascript" src="src="coolmenu.js"></script>

But it doesn't work. My index page is index.shtml and I have a php include working

Thanks,

somerset

10:43 pm on Jan 5, 2004 (gmt 0)

10+ Year Member



js includes can be local or absolute addresses such as:

<script language="JavaScript" src="http://www.mydomain.co.uk/myfile.js"></script>

<script language="JavaScript" src="myfile.js"></script>

the actual file does not have the script language="JavaScript"head and foot, but rather goes straight into the scripting code.

TrinkDawg

11:11 pm on Jan 5, 2004 (gmt 0)

10+ Year Member



I don't know if this is a typo from your post or if it's in your html too, but you have src="src="coolmenu.js" in your script tag. Also, try making sure that this declaration is in the <head></head> section of your page.

Acternaweb

3:27 pm on Jan 6, 2004 (gmt 0)

10+ Year Member



Thanks, I tried the above and it didn't work. And fixed the typo, thanks for finding that

TrinkDawg

4:16 pm on Jan 6, 2004 (gmt 0)

10+ Year Member



Try this:

<script language="JavaScript" src="coolmenu.js"></script>

I have never put the type in the script tag like you had, and I always capitalize the J and S in JavaScript. It would be wierd if that were your problem, but who knows? Also, make sure that the coolmenu.js file is in the same directory as the file generating your html. It may be a path problem.

Sinner_G

4:21 pm on Jan 6, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Are you certain the problem is with the include and not with the JS in that file? Try a simple document.write and see if that works.

@TrinkDawg: Funny, I always do exactly the opposite, include the type but not the language...

korkus2000

6:26 pm on Jan 6, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Also don't put script tags in the js file itself. It should be strictly javascript.

Miki

2:58 am on Jan 8, 2004 (gmt 0)

10+ Year Member



I used to use Javascript includes before I had access to PHP. This article I wrote has a short tutorial on what you'll need to get your include working. Hope it helps. :)

Essentially, the line that calls your include.js is calling your .js contents as Javascript code. So your include.js needs to use Javascript only to output your content.

What Sinner_G said - use document.write. :)

Just curious - how come you want to use Javascript includes if you've got PHP happening? IMO, server side is heaps better!

[edited by: jatar_k at 1:38 pm (utc) on April 16, 2008]