getElementsByName() - opera x firefox

Discussion in 'Javascript' started by André Wagner, Apr 20, 2006.

  1. I'm trying to get all the "divs" that have a given NAME using
    getElementsByName(). For example, the following code:

    <html>
    <head>
    <script type="text/javascript">

    function on_load()
    {
    var pages = document.getElementsByName("name");
    alert(pages.length);
    }

    </script>
    <body onload="on_load()">
    <p name="name" id="id">Teste</p>
    </body>
    <html>

    when I open this page in Firefox, it gives me a popup saying "1", that
    is correct. But if I do it in Opera, it gives me "0".

    Testing, I found out that changing the line to
    var pages = document.getElementsByName("id");
    in Opera gives me the correct result. So the Opera function
    getElementsByName() returns me the objects, not according the NAME, but
    according the ID. (which is wrong, according to
    http://www.w3.org/TR/DOM-Level-2-HTML/html.html#ID-71555259).

    Am I speaking nonsense here, or is Opera doing the wrong thing? In this
    case, how can I have all objects according to the NAME? (no, I can't
    just use the ID)

    Thank you in advance.

    André
    André Wagner, Apr 20, 2006
    #1
    1. Advertising

  2. On 20/04/2006 20:25, André Wagner wrote:

    > I'm trying to get all the "divs" that have a given NAME using
    > getElementsByName().


    A div element cannot have a name attribute. End of story.

    [snip]

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


    HTML documents require a title element.

    [snip]

    > </script>
    > <body onload="on_load()">


    Though the end-tag for head elements is optional, it's generally a good
    to include it.

    [snip]

    > <html>


    An end-tag, perhaps? :)

    > when I open this page in Firefox, it gives me a popup saying "1", that
    > is correct.


    Correction: it's what you expect.

    [snip]

    > Am I speaking nonsense here,


    Yes.

    > or is Opera doing the wrong thing?


    Opera is behaving very reasonably.

    > In this case, how can I have all objects according to the NAME?


    You cannot. The getElementsByName method only needs to return elements
    that have a matching name attribute value when a name attribute is
    defined (in the DTD) for that element. The HTML DOM specification refers
    to elements generically because, unlike XHTML, numerous HTML elements
    unrelated to forms (such as anchors, images, frames, etc.) can have name
    attributes.

    What you are experiencing is the difference in error correction
    mechanisms between browsers. You're expecting a browser to take an
    invalid document, then all you to script it in some manner. However, as
    far as invalid documents are concerned, all bets are off. Firefox isn't
    wrong in its behaviour, it's just different from that of Opera.

    > (no, I can't just use the ID)


    Then use the class attribute along with the getElementsByTagName method.

    Mike

    --
    Michael Winter
    Prefix subject with [News] before replying by e-mail.
    Michael Winter, Apr 20, 2006
    #2
    1. Advertising

  3. André Wagner wrote:

    > I'm trying to get all the "divs" that have a given NAME using
    > getElementsByName().


    So far, so good.

    > For example, the following code:


    Is not Valid.

    > <html>


    The DOCTYPE declaration is missing before that.

    > <head>
    > <script type="text/javascript">
    >
    > function on_load()
    > {
    > var pages = document.getElementsByName("name");
    > alert(pages.length);
    > }
    >
    > </script>


    The `title' element is mandatory, and missing here. And although the close
    tag for the `head' element is defined to be optional in HTML, I recommend
    to include it anyway.

    > <body onload="on_load()">


    You should declare the default scripting language (used for intrinsic event
    handler attribute values), within the `head' element:

    <meta http-equiv="Content-Script-Type" content="text/javascript">

    (That is only a recommendation, not a requirement for validity.)

    > <p name="name" id="id">Teste</p>


    A `p' element has no `name' attribute in any Valid (X)HTML version.

    <URL:http://www.w3.org/TR/html4/struct/text.html#edef-P>

    > </body>
    > <html>


    There can be only one (document root) `html' element in a Valid (X)HTML
    document:

    <URL:http://www.w3.org/MarkUp/SGML/productions.html>

    You were looking for </html>, the close tag of that element, instead.

    > when I open this page in Firefox, it gives me a popup saying "1", that
    > is correct.


    That depends. You have not declared a document type. So I do not think
    it is incorrect that Firefox assumes tag soup and therefore
    Document::getElementsByTagName() works without restriction to the HTML
    Specification, or any other markup language specification. However, the
    tag soup itself is not correct at all.

    > But if I do it in Opera, it gives me "0".
    >
    > Testing, I found out that changing the line to
    > var pages = document.getElementsByName("id");
    > in Opera gives me the correct result.


    The correct result would be a reference to a NodeList object which
    `length' property was 0, as there is no element with name "id" here.

    > So the Opera function
    > getElementsByName() returns me the objects, not according the NAME, but
    > according the ID.


    Not at all. You will observe that a reference to a NodeList object
    is returned, and that its `length' property has the value 0 (no
    matching element). Tested with Opera/8.54 (X11; Linux i686; U; en).

    > (which is wrong, according to
    > http://www.w3.org/TR/DOM-Level-2-HTML/html.html#ID-71555259).


    Ex falso quodlibet: Your markup is not a Valid HTML 4.01 or XHTML 1.0
    document. A DOM implementation can work as it wants without violating
    the W3C DOM Specification because the latter simply does not apply here.

    > Am I speaking nonsense here,


    Partially.

    > or is Opera doing the wrong thing?


    No, it is not. Neither is Firefox, AIUI.

    > In this case, how can I have all objects according to the NAME? (no, I
    > can't just use the ID)


    Use Valid markup only. <URL:http://validator.w3.org/>


    PointedEars
    Thomas 'PointedEars' Lahn, Apr 20, 2006
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. simon

    document.getElementsByName problem

    simon, May 9, 2005, in forum: ASP .Net
    Replies:
    5
    Views:
    26,254
    simon
    May 10, 2005
  2. Bob
    Replies:
    24
    Views:
    1,529
  3. AR
    Replies:
    2
    Views:
    186
  4. mitch
    Replies:
    3
    Views:
    253
    Richard Cornford
    Nov 14, 2005
  5. briggs
    Replies:
    20
    Views:
    446
    Randy Webb
    Jan 17, 2006
Loading...

Share This Page