# Possible to preload a .js file?



## CHwebby (Sep 29, 2006)

Is it possible to use a preload script of some kind to preload a .js file? I'm using a Random Image/Link javacript script in a .js file to link to a random affiliate on every page within my site. Hence the reason why I place it in a .js file. Otherwise I would have to alter the script on every single page when a new affiliate is added. The .js file does slow down the loading of the site some. Enough for me to notice. So I was wondering what the possibilities of getting the .js file to preload and if it would even make a difference.


----------



## namenotfound (Apr 30, 2005)

Does your host have server-side includes (SSI)? Because if it does, that would be a better approach to your affiliate links (rather than using javascript). This would also mean your page would load faster.


----------



## CHwebby (Sep 29, 2006)

Hmmmm not sure if it does. I'll have to check on that first. I don't know a thing about SSI


----------



## namenotfound (Apr 30, 2005)

If your host has SSI, you can activate it by giving your site the extension .shtml

What you do is throw your affiliate html inside another file (even a regular text file) and include it into your page with the following


```
<!--#include file="affiliate.txt"-->
```
That is to include a file that is in the SAME directory as the include.

You can also choose a path with the folllowing


```
<!--#include virtual="includes/affiliate.txt" -->
```
This is probably what you'll want, then you can include a file no matter which directory it's in. So you could have one affiliate file in root (or another directory) and include it to all your pages.


----------



## MMJ (Oct 15, 2006)

Don't preload it, load it after everything else has finished loading.


----------



## haswalt (Nov 22, 2004)

or to make things faster you could get the server to gzip the javascript file up and then aggresivly cash it.

depends on server setup though and also make sure you have optimized the flash file. run it thorugh http://www.julienlecomte.net/yuicompressor/ or some such.

if you don't know this does this:

*Before:*

```
/*
Annual Occasions Count Down script- By JavaScript Kit
For this and over 400+ free scripts, visit http://www.javascriptkit.com
This notice must stay intact
*/

var today=new Date()

//Enter the occasion's MONTH (1-12) and DAY (1-31):
var theoccasion=new Date(today.getFullYear(), 12, 25)

//Customize text to show before and on occasion. Follow grammer below:
var beforeOccasionText="left until Christmas"
var onOccasiontext="Today is Christmas. Happy Holidays!"

var monthtext=new Array("Jan","Feb","Mar","April","May","June","July","Aug","Sep","Oct","Nov","Dec")
theoccasion.setMonth(theoccasion.getMonth()-1) //change to 0-11 month format
var showdate="("+monthtext[theoccasion.getMonth()]+" "+theoccasion.getDate()+")" //show date of occasion

var one_day=1000*60*60*24
var calculatediff=""

calculatediff=Math.ceil((theoccasion.getTime()-today.getTime())/(one_day))
if (calculatediff<0){ //if bday already passed
var nextyeartoday=new Date()
nextyeartoday.setFullYear(today.getFullYear()+1)
calculatediff=Math.ceil((nextyeartoday.getTime()-today.getTime())/(one_day)+calculatediff)
}

//Display message accordingly
var pluraldayornot=(calculatediff==1)? "day" : "days"
if (calculatediff>0)
	document.write("<b>"+calculatediff+" "+pluraldayornot+" "+beforeOccasionText+" "+showdate+"!</b>")
else if (calculatediff==0)
	document.write("<b>"+onOccasiontext+" "+showdate+"!</b>")
```
*After:*

```
var today=new Date();var theoccasion=new Date(today.getFullYear(),12,25);var beforeOccasionText="left until Christmas";var onOccasiontext="Today is Christmas. Happy Holidays!";var monthtext=new Array("Jan","Feb","Mar","April","May","June","July","Aug","Sep","Oct","Nov","Dec");theoccasion.setMonth(theoccasion.getMonth()-1);var showdate="("+monthtext[theoccasion.getMonth()]+" "+theoccasion.getDate()+")";var one_day=1000*60*60*24;var calculatediff="";calculatediff=Math.ceil((theoccasion.getTime()-today.getTime())/(one_day));if(calculatediff<0){var nextyeartoday=new Date();nextyeartoday.setFullYear(today.getFullYear()+1);calculatediff=Math.ceil((nextyeartoday.getTime()-today.getTime())/(one_day)+calculatediff)}var pluraldayornot=(calculatediff==1)?"day":"days";if(calculatediff>0){document.write("<b>"+calculatediff+" "+pluraldayornot+" "+beforeOccasionText+" "+showdate+"!</b>")}else{if(calculatediff==0){document.write("<b>"+onOccasiontext+" "+showdate+"!</b>")}};
```
not the best example but a quick one. See for example what happens when mootools is run through it. www.mootools.net (caompare uncompressed version with YUI compressed version)

Harry


----------

