View Full Version : Cannot read src array

07-09-2005, 08: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 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>
End of Test 1



07-09-2005, 09: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>

07-09-2005, 10: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.

07-09-2005, 10: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>')
<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.

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

That got it.