...

View Full Version : Having one js file call another js file?



PhotoJoe47
10-22-2005, 12:11 AM
I just now learning how to create and use js files.

I was wondering if you can have one js file have a call to another js file in it.

I tried putting this as the the last line of a js file:

<script type="text/javascript" src="tumbnail.js"></script>

I know you are normally suppose to leave off the <script></script> tags in the js file, but I thought I would try it anyway. Of course it didn't work, so if you can call another js file from with in the js file, how do you do it.

PhotoJoe

gph
10-22-2005, 12:39 AM
you can document.write one file from another



document.write('<script type="text/javascript" src="tumbnail.js"><\/script>')

which happens immediately

Or you can bring a file in when needed



function newScript(js_src){
var js=document.createElement('script');
js.type='text/javascript';
js.src=js_src;
document.getElementsByTagName('head')[0].appendChild(js)
}

Basscyst
10-22-2005, 01:14 AM
Or you can take the actual code from the thumbnail file and paste it into the bottom of the other include file.

Basscyst

PhotoJoe47
10-22-2005, 02:22 AM
Thanks "gph",

Your first example is just what I was looking for. I will add the second one to my list of tips I'm saving in a word.doc for reference.

Thanks "Basscyst",

The first js file contains the variables and functions that are unique to that page. The second js file is for functions that will be used for all my tumbnail pages.

I understand that once a js file has been loaded to the browser it is stored in the cache. So after the first tumbnail page has been display the javascript functions that are common to all my tumbnail pages will load from the browser cache and not the server which will save time and I think bandwidth use?

The reason I want to have a js file for the variables and functions that are unique to each page is because I'm planning on finding away to use the same tumbnail page over and over again instead of creating 50 different tumbnail pages. I'm doing research on that problem right now.

BTW I just publish the website that I have been working on:

http://www.photojoe.ws

If you want to see the tumbnail page that I have been working on, just choose the "Catalog of Pictures" from the menu on the left.

PhotoJoe



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum