Web Development

General discussion


why javascript fails-external .js file?

By mail ·
So, this script I'm using on my homepage
(http://danmoriarty.com), to rotate .jpg images,
that only seems to work when it's embedded
in that page's code.

When I try to add it to my external .js file, with
my other javascript code, it fails to work.
Anyone know why?

Here's the code:
<script language="JavaScript"

webImages = new
webURL = new
thisSite = 0
imgCt= webImages.length

function rotate() {
if (document.images){
if (document.webBanner.complete){
if (thisSite ==imgCt){
thisSite= 0

setTimeout("rotate()", 4 * 1000)

function newLocation() {
document.location.href =
"http://danmoriarty.com/" + webURL[thisSite]


<body onLoad="rotate()">
<a href="javascript:newLocation()"> <img
name="webBanner" alt="websites"

I'm trying to learn javascript. Thanks much.

This conversation is currently closed to new comments.

Thread display: Collapse - | Expand +

All Comments

Collapse -

relative URLs, maybe

by Jay Eckles In reply to why javascript fails-exte ...

This is sort of a shot in the dark, but the problem may be that you use relative URLs for the images in your Javascript. If the script is embedded in the page, then the relative URLs resolve to what you expect. If the script is included from an external file, the relative URLs may resolve based on the directory where the .js file resides.

I'm not sure if that's correct or not, but I would try using fully qualified URLs in the script and see if that solves the problem.

Good luck.


Collapse -

my thoughts exactly

by John Wilker In reply to relative URLs, maybe

I was just working with some JS code and it is in an included file at the same level as the file calling it. Except that the file doing the calling of the include is one level up so the link to the JS needed to be correct based on the main page. I'dwalk through all the links to the images and look at the folder layout, make sure that everything is where the JS file thinks it should be.


Related Discussions

Related Forums