Click to See Complete Forum and Search --> : Keeping all the functions together


CherryAA
12-02-2003, 12:33 PM
There are about half a dozen Javascript functions which almost every page of my site uses. At the moment, they make an immense block of code in the head of each page.

It seems more efficient to put all of the functions together in one page, then let each user-facing page call them from there as required.

Trouble is, I don't know how to phrase the command to fetch the function. Writing the whole path in before the function name doesn't seem to work.

I realise this must be very basic, but please help!
Thanks,
CAA

TheBearMay
12-02-2003, 12:41 PM
You could try putting all of the functions into one JS file (eg. myFunctions.js) and then place the line below in the <head> section:

<script src='myFunctions.js'></script>

Then reference the functions normally. The JS file would contain everything normally found between the <script> and </script> tags.

AdamGundry
12-02-2003, 12:43 PM
If you put the functions in a file on their own (only Javascript code in the file and with a .js extension), you can load the external script using:

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

Note that the script block should be completely empty. This will then load the .js file when execution reaches that point.

Adam

Edit: TheBearMay got there first, but note the syntax I gave is required for standards compliance.

CherryAA
12-05-2003, 10:42 AM
It works beautifully, and all my pages are now about a quarter of the size!

Cheers :)
Cherry