Welcome to WebmasterWorld Guest from 35.172.111.215

Forum Moderators: open

Message Too Old, No Replies

Javascript "include," is it possible?

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

Preferred Member

10+ Year Member

joined:Jan 16, 2001
posts:585
votes: 0


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,

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

Junior Member

10+ Year Member

joined:Dec 13, 2003
posts:101
votes: 0


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.

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

New User

10+ Year Member

joined:Dec 11, 2003
posts:20
votes: 0


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.
3:27 pm on Jan 6, 2004 (gmt 0)

Preferred Member

10+ Year Member

joined:Jan 16, 2001
posts:585
votes: 0


Thanks, I tried the above and it didn't work. And fixed the typo, thanks for finding that
4:16 pm on Jan 6, 2004 (gmt 0)

New User

10+ Year Member

joined:Dec 11, 2003
posts:20
votes: 0


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.

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

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:June 27, 2002
posts:959
votes: 0


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...

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

Senior Member

WebmasterWorld Senior Member korkus2000 is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Mar 20, 2002
posts:3732
votes: 0


Also don't put script tags in the js file itself. It should be strictly javascript.
2:58 am on Jan 8, 2004 (gmt 0)

New User

10+ Year Member

joined:Jan 8, 2004
posts:23
votes: 0


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]