Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Javascript How-To
|
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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old August 7th, 2005, 12:36 AM
Registered User
 
Join Date: Aug 2005
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"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>JavaScript Testing</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

</head>

<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);
    }
}

</script>

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

</body>
</html>
 
Old August 7th, 2005, 12:16 PM
Friend of Wrox
 
Join Date: Oct 2004
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
Default

Hii threealligator!!1


for(i=0;i<links.length;i++)
alert(links(i))

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 :)

vinod





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





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