Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

External .js problem

Status
Not open for further replies.

vacklin

Programmer
Nov 13, 2001
22
US
Hello,

I am trying take out my Javascript code from the <head> section of my source code and use external .js files.

The javascript works fine when in my <head> but when I place it in an external .js file I get this error:

Line 25
Char 1
Error Unterminated comment
URL file://C:

I have the correct path to the .js file and am using:

<script src="customer.js"></script>

in the <body> to call the script.

I know the error is "Unterminated comment" on line 25 but there is no line 25 in the external .js file. Here is the exact external file:

var theImages = new Array()

theImages[0] = 'theImages[1] = 'theImages[2] = 'theImages[3] = 'theImages[4] = 'theImages[5] = '
var j = 0
var p = theImages.length;
var preBuffer = new Array()
for (i = 0; i < p; i++){
preBuffer = new Image()
preBuffer.src = theImages
}
var whichImage = Math.round(Math.random()*(p-1));
function showImage(){
document.write('<img src="'+theImages[whichImage]+'">');
}

Is there something I'm doing wrong here? I would really appreciate some help. I've looked at this and tried different things so long, my eyes are crossed!

Thank you!
Vickie
 
Change this

Code:
<script src="customer.js"></script>

to this

Code:
<script src="customer.js" language="Javascript" type="text/Javascript">
</script>

and keep this in the <head> section not the <body> section (although i'm not sure if it will make a difference).

hth

Stewart
 
You can include external JS files anywhere in the HTML page, just like including a <script language="javascript"></script> section.

Lee
 
Stewart,

Thanks for your help but I had already tried using:

<script src="customer.js" language="Javascript" type="text/Javascript">
</script>

It doesn't help. Also, the script must be called from the body and not the head since it needs to show an image in the body. Javascripts can be run from the body and well as the head. If I call the script from the head section instead of using it in the body what would I use in the body where the images should appear?

The error I am getting is showing there is an error in the Javascipt in the external file not on the HTML page that calls the script, I think. The Javascript works perfectly when it's not in an external file. When I place it in the external file is when I start getting errors.

I get the same exact error code for any javascript that I place in external files so it must be some code or comment I need to add or remove from the external .js files.

Come on now! I know all you javascript experts know the answer. I think this is something very simple I am overlooking and I really appreciate anyone's help. Thanks in advance.

Vickie
 
The error message is referencing a file on C:\, but there's no file named. Have you tried using Netscape and debugging it through there? I like that browser for tracking down JS errors a LOT better than IE.

Lee
 
Thank you for your help Stewart and Lee. I have the problem solved. It seems to work with calling the script in the head section but using some code in the body where the images need to appear. I had an error in that body code.

That's what happens sometimes when you've looked at a problem too long. I should have gone to bed and tried again today instead of staying up all night determined to find a fix.

I sure appreciate your help. Thank you!

Vickie
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top