...

View Full Version : links.length return 0



bluephoenix
04-10-2003, 09:05 PM
I have a test HTML page with 5 links within it. The JavaScript's document.links.length returns 0. Any thoughts?

-Tim

brothercake
04-10-2003, 09:50 PM
Only one thought - the links collection is deprecated; it shouldn't be used. Do this instead:

var aTags = document.getElementsByTagName("a");
var aTagsLen = aTags.length;

HairyTeeth
04-10-2003, 09:57 PM
Just out of interest...


<html>
<head>
<title>Demo</title>
<script type="text/jav&#97;script" language="jav&#97;script">
<!--;
function getLinks(){
var linkElm,i=0;
while(linkElm=document.links[i++]){
//alternative alert...
//alert("a href = " + linkElm + "\nLink Index: " + i)

alert("a href = " + unescape(linkElm) + "\nIndex: " + i)
var linksLength = document.links.length
}
alert("Number of links: " + linksLength)
}
//-->
</script>
</head>
<body>
<a href="jav&#97;script:void getLinks()">getLinks</a>
<a href="foo_1.html">Foo</a>
<a href="foo_2.html">Foo</a>
<a href="foo_3.html">Foo</a>
<a href="foo_4.html">Foo</a>
</body>
</html>

cheesebagpipe
04-10-2003, 09:58 PM
Are DOM Level 0 host collections deprecated? Didn't know that.

bluephoenix - are you running this code in the <head>er? The links[] collection isn't fully populated until all the HTML has been parsed.

brothercake
04-10-2003, 10:23 PM
Originally posted by cheesebagpipe
Are DOM Level 0 host collections deprecated? Didn't know that.
Well ... I'm going out on a limb here ... I may be completely wrong, but as I understand it:

DOM-0 collections are based on name semantics, but the name attribute is deprecated; since name has no semantics in an XHTML document the returned collection should always be empty.

liorean
04-10-2003, 10:44 PM
Not the DOM0 collections that got included in DOM2 HTML at least...

interface HTMLDocument : Document {
readonly attribute HTMLCollection images;
readonly attribute HTMLCollection applets;
readonly attribute HTMLCollection links;
readonly attribute HTMLCollection forms;
readonly attribute HTMLCollection anchors;
};

interface HTMLFormElement : HTMLElement {
readonly attribute HTMLCollection elements;
};

interface HTMLSelectElement : HTMLElement {
readonly attribute HTMLOptionsCollection options;
};





HTMLCollection
namedItem
This method retrieves a Node using a name. With [HTML 4.01] documents, it first searches for a Node with a matching id attribute. If it doesn't find one, it then searches for a Node with a matching name attribute, but only on those elements that are allowed a name attribute. With [XHTML 1.0] documents, this method only searches for Nodes with a matching id attribute. This method is case insensitive in HTML documents and case sensitive in XHTML documents.
Parameters

name of type DOMString The name of the Node to be fetched.

Return Value

Node


The Node with a name or id attribute whose value corresponds to the specified string. Upon failure (e.g., no node with this name exists), returns null.
No Exceptions

HTMLOptionsCollection
namedItem
This method retrieves a Node using a name. It first searches for a Node with a matching id attribute. If it doesn't find one, it then searches for a Node with a matching name attribute, but only on those elements that are allowed a name attribute. This method is case insensitive in HTML documents and case sensitive in XHTML documents.
Parameters

name of type DOMString
The name of the Node to be fetched.

Return Value

Node


The Node with a name or id attribute whose value corresponds to the specified string. Upon failure (e.g., no node with this name exists), returns null.
No Exceptions

See? They are primarily id connected, secondarily name connected, but only on elements that support name attribute.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum