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!

Dynamic array with js file

Status
Not open for further replies.

EZEason

Programmer
Dec 11, 2000
213
US
The app is asp.net, with some javascript. I have a JS file that looks for an array and creates a news link that fades in and out, rotating through my array. They array is dynamicly build in an asp.net/vb.net webcontrol and looks like this:
Code:
<SCRIPT language="javascript" type="text/javascript">
nNews=new Array()
nNews[0]=New Array()
nNews[0] ["text"]="New Organization"
nNews[0] ["link"]="#"
nNews[1]=New Array()
nNews[1] ["text"]="Employee Purchase Program"
nNews[1] ["link"]="#"
nNews[2]=New Array()
nNews[2] ["text"]="Pre-Tax Form"
nNews[2] ["link"]="#"
</SCRIPT>

Then I add in the JS file like this:
Code:
<script src="reminder.js"language="javascript"></script>

As you can tell this will display company news that is populated from a SQL database. The problem I'm having is getting the dynamic array in my set of code to be read in my JS file. I get 2 errors "nNews is not defined" and "Error: expected';' Line 139". What am I doing wrong???

The JS file works fine if I hard code the array and just add it into my web control.

Jim


What doesn't kill you makes you stronger.
 
Looks like both errors are occurring because JavaScript requires a semicolon at the end of every line of code. This results in nNews not being defined and in an expected ';'

At least I hope it's that simple :)
 
I tried that, but still get the same error.

What doesn't kill you makes you stronger.
 
This may help, it's the code view from my page. The JS appears at the bottom of my page.
Code:
</div>
<SCRIPT language="javascript" type="text/javascript">
nNews=new Array();
nNews[0]=New Array()
nNews[0] ["text"]="New Organization"
nNews[0] ["link"]="#"
nNews[1]=New Array()
nNews[1] ["text"]="Employee Purchase Program"
nNews[1] ["link"]="#"
nNews[2]=New Array()
nNews[2] ["text"]="Pre-Tax Bonus Form"
nNews[2] ["link"]="#"
</SCRIPT>
<script src="reminder.js"language="javascript"></script></form>
</body>
</html>

What doesn't kill you makes you stronger.
 
Are your code examples copied and pasted? If so, you need to remember that Javascript is case sensitive, and the keyword new is NOT the same as New that you use in several of your lines.

If your code is NOT copied and pasted in here for us to view, please do that so we can see exactly what the browser is working with.

I always use semicolons at the end of lines, though it's not absolutely necessary with JS.

I would also write the example as
Code:
var ni = 0;
nNews[ni++]={text:"New Organization", link:"#"};
nNews[ni++]={text:"Employee Purchase Program", link:"#"};
nNews[ni++]={text:"Pre-Tax Bonus Form", link:"#"};

Properites of objects in Javascript can be accessed like an associative array using the variable name as the array index, as well as using the dot operator:

nNews[0]['text'];
nNews[0].text;

Lee
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top