Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New Coder
    Join Date
    Dec 2002
    Location
    Central New York
    Posts
    87
    Thanks
    0
    Thanked 0 Times in 0 Posts

    links.length return 0

    I have a test HTML page with 5 links within it. The JavaScript's document.links.length returns 0. Any thoughts?

    -Tim
    Timothy Boronczyk

  • #2
    Senior Coder
    Join Date
    Jun 2002
    Location
    near Oswestry
    Posts
    4,508
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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;
    "Why bother with accessibility? ... Because deep down you know that the web is attractive to people who aren't exactly like you." - Joe Clark

  • #3
    Regular Coder
    Join Date
    Mar 2003
    Posts
    176
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Just out of interest...
    Code:
    <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>

  • #4
    Regular Coder
    Join Date
    Nov 2002
    Posts
    596
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #5
    Senior Coder
    Join Date
    Jun 2002
    Location
    near Oswestry
    Posts
    4,508
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.
    Last edited by brothercake; 04-10-2003 at 10:40 PM.
    "Why bother with accessibility? ... Because deep down you know that the web is attractive to people who aren't exactly like you." - Joe Clark

  • #6
    Master Coder
    Join Date
    Feb 2003
    Location
    UmeŚ, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    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.
    Last edited by liorean; 04-10-2003 at 10:56 PM.
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •