View Full Version : external .js files
Azzkika
06-28-2002, 02:51 PM
Hi all,
OK, I have a number of roll-overs coded into my site pages. I have been able to ship out the functions into a single external .js file without a problem.
However, when I try and remove the image definitions and .src references for the rollover images, it doesn't work. Does anybody know if I can ship out something like:
if (document.images)
{ image1 = new Image; image2 = new image;
image1.src = "../images/image1.gif"; image2.src = "../images/image2.gif" }
into an external .js file? It doesn't seem to be working for me and I was wondering if there was a workaround or if I can't do it at all.
Thanks, :)
Azz
bcarl314
06-28-2002, 03:04 PM
Remeber the new Image is a constructor, it should be new Image();
Add the parens, and remember the "I" needs to be caps.
That should do it for you.
You also might try changing the if to:
if(document.images.length>0)
Azzkika
06-28-2002, 04:11 PM
Hi there,
Thanks for that - sorry I typed it in wrong.
I seemed to have solved my problem, though - there were a few comments in the code being copied across - I removed these and it worked beautifully!
Can you not comment external .js files then?
Thanks for the input
Azz
boxer_1
06-28-2002, 04:25 PM
Originally posted by Azzkika
.....Can you not comment external .js files then?
Thanks for the input
Azz
You can comment in external .js files, as long as you don't comment out the wrong thing :D .
For example, in your .js file you could comment like this and you'll still get the alert:
alert('Hello World!'); // This is an alert
But this would comment out the alert:
// alert('Hello World!');
For multiple lines:
/*The following is
a simple alert
of Hello World*/
alert('Hello World!');
What did the comments in your.js file look like ;)?
Azzkika
06-28-2002, 04:35 PM
Hmmm...
That's interesting.
:confused:
The comments were like so:
if (document.images)
{
// comment here
PjMOff = new Image;
PjMOn = new Image;
BAOff = new Image;
BAOn = new Image;
PgMOff = new Image;
PgMOn = new Image;
// comment here
PjMOff.src = "../images/PjMIconOff.gif"
PjMOn.src = "../images/PjMIconOn.gif"
BAOff.src = "../images/BAIconOff.gif"
BAOn.src = "../images/BAIconOn.gif"
PgMOff.src = "../images/PgMIconOff.gif"
PgMOn.src = "../images/PgMIconOn.gif" }
It should have been OK, yeah?
Azz
boxer_1
06-28-2002, 04:46 PM
Should have been, but as bcarl314 mentioned 'new Image' should be 'new Image()'. You say after you removed the comments it worked fine. Are you sure it's not just because the images are cached, making the rollovers appear as though they were instant because it was drawing the image from the cache?
If you haven't already, try clearing your cache/tmp internet files and see if the rollovers are still instantaneous ;).
Azzkika
06-28-2002, 04:55 PM
Strange...
did the full cache clear out thing and it didnt seem to make any difference to the rollover time - no delays at all. Oh well ... I'll try adding those paretheses and see what happens ...
Thanks for the help once again :thumbsup:
Azz
bcarl314
06-28-2002, 05:03 PM
2 other quick things
1) About the comments, make sure that if your using a text-editor and you have word wrap on that the comments are on new lines. (Just put a return in if you need to)
2) If you are testing this on our own box, your computer is fast enough talking to itself that it may appear the roll-overs are instant, but when you upload over a server and need to connect at a slower speed, you'll see the change.
Azzkika
06-28-2002, 05:10 PM
interesting...
I'll test it on my 56K line on my PII 200 at home later - that should show up any slowness.
Have a look at the site and see if you guys can detect anything (just don't use NS whatever you do - haven't optimised for that yet!) - the main index page is the one with the external .js!
www.nemeaconsulting.co.uk (http://www.nemeaconsulting.co.uk)
Cheers, guys - thanks for that!
Azz
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.