array of links

T

TJJay3rd

hello, all

i'm new to javascript and am attempting to write my first array for a
menu / links.

----source-----
<body>
<script type="text/javascript">

var HorizontalLinks=new Array()

/* <a title="screen tip" href="#1">displayed title</a> */

HorizontalLinks[0]="Yahoo", "www.yahoo.com", "Yahoo";
HorizontalLinks[1]="AOL", "www.aol.com", "AOL";
HorizontalLinks[2]="T. J. jay", "www.geocities.com", "My Site";



for (i=0; i<HorizontalLinks.length; i++)

{

document.write('<a href="http://' + HorizontalLinks[1])+ '">'+
HorizontalLinks[0] + '</a>');

}



</script>

----end----

can someone with patience help me? TIA
 
E

Evertjan.

wrote on 11 okt 2007 in comp.lang.javascript:
var HorizontalLinks=new Array()

/* <a title="screen tip" href="#1">displayed title</a> */

HorizontalLinks[0]="Yahoo", "www.yahoo.com", "Yahoo";
HorizontalLinks[1]="AOL", "www.aol.com", "AOL";
HorizontalLinks[2]="T. J. jay", "www.geocities.com", "My Site";

var HorizontalLinks =
[
['Yahoo', 'www.yahoo.com', 'Yahoo'],
['AOL', 'www.aol.com', 'AOL'],
['T. J. jay', 'www.geocities.com', 'My Site']
];

alert(HorizontalLinks[1][1]) // www.aol.com
 
T

Thomas 'PointedEars' Lahn

<body>
<script type="text/javascript">

var HorizontalLinks=new Array()

/* <a title="screen tip" href="#1">displayed title</a> */
HorizontalLinks[0]="Yahoo", "www.yahoo.com", "Yahoo";
HorizontalLinks[1]="AOL", "www.aol.com", "AOL";
HorizontalLinks[2]="T. J. jay", "www.geocities.com", "My Site";

for (i=0; i<HorizontalLinks.length; i++)
{
document.write('<a href="http://' + HorizontalLinks[1])+ '">'+
HorizontalLinks[0] + '</a>');

}

</script>

can someone with patience help me? TIA


You have not stated what your problem with that code is, and is it unlikely
that you have you cared to consult any documentation before you posted. See
http://jibbering.com/faq/

But if I were to make an educated guess anyway (and so demonstrate the
patience that you asked for *this time*), I would assume you wonder whether
the above prints either a list of items like

<a href="http://a">Y</a>

in Mozilla or a list of items like

<a href="http://undefined">undefined</a>

in Internet Explorer-based user agents.

The reason for that would be that you have created an array, but you have
not created an array of arrays. Instead, you have created an array of
strings. Different evaluation processes aside, your code is equivalent to

HorizontalLinks[0]= "Yahoo";
HorizontalLinks[1]= "AOL";
HorizontalLinks[2]= "My Site";

because in simple expressions the comma is an operator that evaluates the
left-hand operand, and makes the evaluation of the right-hand operand the
result of the entire expression. Hence the second bracket property accessor
subscript in your code denotes a character of the remaining string value in
JavaScript, and an undefined property of the implicitly created String
object in JScript.

You were then looking for

var HorizontalLinks = new Array(
new Array("Yahoo", "www.yahoo.com", "Yahoo");
new Array("AOL", "www.aol.com", "AOL");
new Array("T. J. jay", "www.geocities.com", "My Site"));

instead. And I would recommend that you used leading capitals in
your identifiers only where they specify a constructor function.

Furthermore, you have to escape the character sequence "</" (ETAGO -- End
Tag Open -- delimiter) within the HTML `script' element (and other HTML
elements with CDATA content model, such as the `style' element). This is
best done here as follows:

document.write('<a href="http://' + horizontalLinks[1])+ '">'+
horizontalLinks[0] + '<\/a>');

And finally, you could make that much more efficient, e.g. by using an
output array:

var out = new Array();

for (var i = 0, len = horizontalLinks.length; i < len; i++)
{
var item = horizontalLinks;

out.push(
new Array('<a href="http://', item[1], '">', item[0], '<\/a>')
.join(""));
}

document.write(out.join("\n"));

And it became easier to maintain if you used an array of Object objects instead:

var
horizontalLinks = [
{text: "Yahoo", domain: "www.yahoo.com", description: "Yahoo"},
{text: "AOL", domain: "www.aol.com", description: "AOL"},
{text: "T. J. jay", domain: "www.geocities.com",
description: "My Site"}
],

out = [];

for (var i = 0, len = horizontalLinks.length; i < len; i++)
{
var item = horizontalLinks;
out.push(['<a href="http://', item.domain, '">', item.text,
'<\/a>'].join(""));
}

document.write(out.join("\n"));

That said, it is a bad idea to do all of this in a production environment
because users will see nothing without client-side script support. So you
should consider it a programming exercise only.


HTH

PointedEars
 
T

Thomas 'PointedEars' Lahn

Thomas said:
var HorizontalLinks=new Array()

/* <a title="screen tip" href="#1">displayed title</a> */
HorizontalLinks[0]="Yahoo", "www.yahoo.com", "Yahoo";
HorizontalLinks[1]="AOL", "www.aol.com", "AOL";
HorizontalLinks[2]="T. J. jay", "www.geocities.com", "My Site";

for (i=0; i<HorizontalLinks.length; i++)
{
document.write('<a href="http://' + HorizontalLinks[1])+ '">'+
HorizontalLinks[0] + '</a>');

}


[...]
But if I were to make an educated guess anyway (and so demonstrate the
patience that you asked for *this time*), I would assume you wonder whether
the above prints either a list of items like

<a href="http://a">Y</a>

in Mozilla or a list of items like

<a href="http://undefined">undefined</a>

in Internet Explorer-based user agents.

The reason for that would be that you have created an array, but you have
not created an array of arrays. Instead, you have created an array of
strings. Different evaluation processes aside, your code is equivalent to

HorizontalLinks[0]= "Yahoo";
HorizontalLinks[1]= "AOL";
HorizontalLinks[2]= "My Site";

because in simple expressions the comma is an operator that evaluates the
left-hand operand, and makes the evaluation of the right-hand operand the
result of the entire expression. [...]


When reviewing this statement which I had assumed to be certain knowledge,
I was forced to observe that I had not fully understood the comma operator
myself as

var a = [];
a[0] = "1", "2", "3";
a

yields `["1"]' in Firefox/2.0.0.7. However,

(a[0] = "1", "2", "3")

yields "3". My explanation is that with an assignment in a comma operation,
the assignment itself is evaluated as one (left-hand) expression -- as in

(a[0] = "1"), "2", "3";

-- and so the comma operator does not count for the assignment (see
ECMAScript Ed. 3, subsection 11.14). But the result of the entire
expression that the assignment is in can be different than the assigned
value, as described before. Perfectly reasonable as per the spec, yet maybe
counter-intuitive.

The conclusions drawn and suggestions I made later are valid, though.


PointedEars
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,154
Messages
2,570,870
Members
47,400
Latest member
FloridaFvt

Latest Threads

Top