Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Javascript How-To
Password Reminder
| FAQ | Members List | Search | Today's Posts | Mark Forums Read
Javascript How-To Ask your "How do I do this with Javascript?" questions here.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Javascript How-To section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old August 7th, 2005, 12:36 AM
Registered User
Join Date: Aug 2005
Location: , , .
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default weird results with for-in loop

Hi all,

I'm doing one of the exercises in the Beginning Javascript book and I'm getting weird results with a for-in loop. I have a page with 5 links to different sites. What I want to do when the page loads is use a for-in loop to cycle through the links[] array and pop up each link name as an alert. However, I end up getting 3 extra alerts that say "undefined", "item", and "namedItem". I'm confused because links.length gives me 5, but the loop gives me 8 alerts. My code is below. Can someone help? Thanks.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
<title>JavaScript Testing</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">


<body language=JavaScript onload="displaylinks()">

<script language="javascript" type="text/javascript">
function displaylinks()
    var links = document.links;
    var index;

    alert (links.length);

    for (index in links)
        alert (links[index].name);


<a href="http://www.yahoo.com" name="yahoo">Yahoo</a>
<a href="http://www.espn.com" name="espn">ESPN</a>
<a href="http://fantasysports.yahoo.com" name="fantasy">Fantasy Sports</a>
<a href="http://www.hotmail.com" name="hotmail">Hotmail</a>
<a href="www.google.com" name="google"> Google </a>

Reply With Quote
  #2 (permalink)  
Old August 7th, 2005, 12:16 PM
Friend of Wrox
Join Date: Oct 2004
Location: delhi, delhi, India.
Posts: 553
Thanks: 0
Thanked 1 Time in 1 Post
Send a message via MSN to vinod_yadav1919 Send a message via Yahoo to vinod_yadav1919

Hii threealligator!!1


but i m still confusing with ur code

for (index in links)
   alert(index) //Why it is length?? at first time
        alert (links[index].name);

Cheers :)

Reply With Quote

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Edit Query Results in Results Grid druid2112 SQL Server 2005 1 June 28th, 2007 08:49 AM
Weird Concantenation jazzcatone SQL Server 2000 1 November 14th, 2006 10:42 AM
Loop, two columns, results keep movin'! Fed-Exin PHP How-To 0 September 3rd, 2004 08:51 PM
Weird Errors kervinshim Classic ASP Databases 3 April 25th, 2004 04:08 AM

All times are GMT -4. The time now is 11:02 PM.

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.