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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Accessing an array value from an external javascript file

Status
Not open for further replies.

SuperSal

Programmer
Mar 13, 2007
33
Hi everyone, Was wondering how i can access array values from an external javascript file to a html page.

The code works if I have it altogether like below:

var IPAddress = new Array("IPAddress", "SaveRequired();", "text", 40, 60, "txtNum") //text field

<script><input name='+IPAddress[0]+' onChange='+IPAddress[1]+' type='+IPAddress[2]+' id='+IPAddress[0]+' size='+IPAddress[3]+' maxlength='+IPAddress[4]+' class='+IPAddress[5]+' /></script>

but when i move the array into an external javascript file and add the code:

<SCRIPT LANGUAGE="JavaScript" src="Scripts/menuitems.js"></script>

it doesnt access the value in the array...can anyone give me a pointer in the right direction

Thanks
Sally
 
Your original code is invalid. You cannot put markup directly into a <script> tag like this:
Code:
<script>[!]<input name='+IPAddress[0]+' onChange='+IPAddress[1]+' type='+IPAddress[2]+' id='+IPAddress[0]+' size='+IPAddress[3]+' maxlength='+IPAddress[4]+' class='+IPAddress[5]+' />[/!]</script>

I'm assuming that you're seeing it work in IE? IE is cool like that - it's dumbed down to allow mistakes in code to run. What you need to do is use document.write, something like this:
Code:
<script type="text/javascript">

var IPAddress = new Array("IPAddress", "SaveRequired();", "text", 40, 60, "txtNum"); //text field
document.write('<input name='+IPAddress[0]+' onChange='+IPAddress[1]+' type='+IPAddress[2]+' id='+IPAddress[0]+' size='+IPAddress[3]+' maxlength='+IPAddress[4]+' class='+IPAddress[5]+' />';

</script>

By using document.write you can remove the <script> tags and put the code into an external js file and then it should work fine.

-kaht

Lisa, if you don't like your job you don't strike. You just go in every day and do it really half-assed. That's the American way. - Homer Simpson
 
Sorry kaht, I just put that piece of code in to demonstrate what I'm trying to do. The code isnt html its javascript that is rendering html like below:

dstr += '<input name='+IPAddress[0]+' onChange='+IPAddress[1]+' type='+IPAddress[2]+' id='+IPAddress[0]+' size='+IPAddress[3]+' maxlength='+IPAddress[4]+' class='+IPAddress[5]+' />';

Sal
 
Make sure the path & filename to your JS file is correct, and the case is as per the filename (some web servers are case sensitive).

Also make sure you dno't haev "script" tags or HTML comments in your external script file, and that you see no JS errors on the page.

Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Thanks Billy Ray. Basically I had the filepath to the external JS file as:

<SCRIPT LANGUAGE="JavaScript" src="/Scripts/menuitems.js"></script>

when it needed to be:

<SCRIPT LANGUAGE="JavaScript" src="../Scripts/menuitems.js"></script>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top