General discussion

Locked

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"
type="text/javascript">

webImages = new
Array("images_websites/150_clh.jpg",
"images_websites/150_primo.jpg",
"images_websites/150_lexmar.jpg")
webURL = new
Array("portfolio_web/web.php?body=clh",
"portfolio_web/web.php?body=primo",
"portfolio_web/web.php?body=lexmar")
thisSite = 0
imgCt= webImages.length

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

document.webBanner.src=webImages[thisSit
e]
}
setTimeout("rotate()", 4 * 1000)
}
}

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

}
</script>

also:
<body onLoad="rotate()">
and:
<a href="javascript:newLocation()"> <img
src="images_websites/150_clh.jpg"
name="webBanner" alt="websites"
border="0"></a>

I'm trying to learn javascript. Thanks much.

This conversation is currently closed to new comments.

2 total posts (Page 1 of 1)  
| 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.

Jay

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.

J.

Back to Web Development Forum
2 total posts (Page 1 of 1)  

Related Discussions

Related Forums