...

View Full Version : Cannot read src array



Coyote
07-09-2005, 07:56 AM
I have an external file, TestVar.js with a single line.
NewsA("This is a test.","test.pdf","TEST")

Within my HTML I wish to use the array variables from TestVar.js, but can't seem to get them loaded for use. The following is what I have, but doesn't work. If I directly place the NewsA array in the script instead of trying to call it from the js it works just the way I want it, but I really need the external file. What am I doing wrong?

*************

Start of test 1
<div style="text-align: center;"><font color="#ff0000">
<script language="javascript">
var NewsA=new Array();
</script>
<script type="text/javascript" src="TestVar.js" language="javascript"><font color="#ff0000">
var NewsText = NewsA[0];
var NewsPath = NewsA[1];
var NewsLink = NewsA[2];
document.write(NewsText);document.write(" <a href = "+NewsPath+">"+NewsLink+"</a>")</script><br>
</font>
</div>
End of Test 1

*********

Coyote

jscheuer1
07-09-2005, 08:51 AM
NewsA=("This is a test.","test.pdf","TEST")

Why not have contents of TestVar.js be:

var NewsA=new Array();
NewsA=["This is a test.", "test.pdf", "TEST"]
document.write(NewsA[0]+' <a href="'+NewsA[1]+'">'+NewsA[2]+'</a>')and the markup look like this:


<div style="text-align: center;"><font color="#ff0000">
<script type="text/javascript" src="TestVar.js"></script>
<br></font></div>

Coyote
07-09-2005, 09:11 AM
The main reason is that there will be html illiterates who will be editing it. I would like it to have as little in it as possible that can be messed up.

jscheuer1
07-09-2005, 09:45 AM
Well, that's easy. Remember, declaring an array name and its contents equal creates the array. So, TestVar.js can be:

NewsA=["This is a test.", "test.pdf", "TEST"]and the markup:

<div style="text-align: center;"><font color="#ff0000">
<script type="text/javascript" src="TestVar.js"></script>
<script type="text/javascript">
document.write(NewsA[0]+' <a href="'+NewsA[1]+'">'+NewsA[2]+'</a>')
</script>
<br></font></div>You cannot combine the two scripts in the markup because code written between the first script's opening and closing tags is executed before the code in its source file. This is still simpler than what you had and fulfills your requirement.

Coyote
07-09-2005, 06:16 PM
Thanks to all.

That got it.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum