Support getElementsByName like in other browsers

RESOLVED WORKSFORME

Status

()

Core
DOM
--
enhancement
RESOLVED WORKSFORME
7 years ago
7 years ago

People

(Reporter: Rob S., Unassigned)

Tracking

Firefox Tracking Flags

(Not tracked)

Details

(URL)

(Reporter)

Description

7 years ago
User-Agent:       Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:2.0.1) Gecko/20100101 Firefox/4.0.1
Build Identifier: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:2.0.1) Gecko/20100101 Firefox/4.0.1 and Mozilla/5.0 (Windows NT 6.1; rv:2.0.1) Gecko/20100101 Firefox/4.0.1

When the following code at the given URL is run:

   var out1 = document.getElementById("out1");
   if (! document.getElementsByName) {
      if (out1.innerHTML.length <= 1) {
         alert('Sorry, your browser does not support "document.getElementsByName"');
         out1.innerHTML = '(Your browser does not support "document.getElementsByName")';
      }
      return;
   }
   var drinks = document.getElementsByName("drinks");
   ...

In Firefox 4.0 it displays the message:

    Sorry, your browser does not support "document.getElementsByName"

while in other browsers it returns the node list of elements with the name "drinks".


Reproducible: Always

Steps to Reproduce:
1. Go to http://www.HTML-5.com/tags/menu-tag/#examples

2. It should automatically scroll down to the Examples on that page, but if it does not, put the cursor in the address bar and hit Return. (Not honoring the fragment identifier in the URL when the page is initially loaded is a regression from May 2010 filed as bug #645075.)

3. Select one of the "Coffee" or "Tea" radio buttons or one of the "Cream" or "Sugar" check boxes.

4. Note that in Firefox 4.0 (Mac or Windows), the alert message is displayed.

5. Repeat steps 1-3 in another browser such as Safari, Chrome or Opera. Note that it displays text indicating which buttons were selected because it bypassed the code inside the "if" statement and fell through to other code below it.


Actual Results:  
It displays the alert message indicating that "document.getElementsByName" returned false.

Expected Results:  
"document.getElementsByName" should return true so that the code inside "if (! document.getElementsByName)" is bypassed. Then 'document.getElementsByName("drinks")' should return a node list will all of the elements having a 'name="drinks"' attribute.

Updated

7 years ago
Assignee: general → nobody
Component: JavaScript Engine → DOM
QA Contact: general → general
Summary: New feature request: Support getElementsByName like in other browsers → Support getElementsByName like in other browsers

Comment 1

7 years ago
Firefox supports getElementsByName, it's the website that is incorrectly telling you that it's not supported (because they were too lazy to code their example correctly).

http://help.dottoro.com/ljlfpmux.php
Status: UNCONFIRMED → RESOLVED
Last Resolved: 7 years ago
Resolution: --- → WORKSFORME
(Reporter)

Comment 2

7 years ago
I cut-and-pasted the code from:

    http://help.dottoro.com/ljlfpmux.php

into http://www.ExampleOnly.com/getelementsbyname.html

Isn't something supposed to happen when you click on the "Get the checked fruit!" button?  Looking at the code, it appears that it's supposed to pop up an alert, but doesn't.

It's possible that code used to work on HTML 4 or XHTML 1 sites, but no longer works on a site that uses the HTML 5 DOCTYPE:

   <!DOCTYPE html>

>> (because they were too lazy to code their example correctly)

What would you change to make the code "correct"? (Either one - the ljlfpmux.php or the http://www.HTML-5.com/tags/menu-tag/#examples code.)
(Reporter)

Comment 3

7 years ago
In other browsers, clicking on the "Get the checked fruit!" button in the code that was provided from http://help.dottoro.com/ljlfpmux.php does pop up an alert saying "The ... radio button is checked".

>> (because they were too lazy to code their example correctly)

What do you have to code differently in Firefox to get that "fruit" example to work like in other browsers?

I added a couple more alerts to the code that was provided, just to be sure it was the getElementsByName that wasn't working in it:

    http://www.ExampleOnly.com/getelementsbyname2.html

So here are a couple more test cases (B and C, with test case "A" provided above):

Test case B:

1. Go to the following URL:

   http://www.ExampleOnly.com/getelementsbyname.html

2. Click on the "Get the checked fruit!" button.

3. Actual results:

   no alert is displayed

4. Expected results, and actual results in other browsers:

   Displays an alert saying:
      "The 1. radio button is checked"


Test case C (showing where the code goes awry):

1. Go to the following URL:

   http://www.ExampleOnly.com/getelementsbyname2.html

2. Click on the "Get the checked fruit!" button.

3. Actual results:

   Displays only one alert saying:
      "before getElementsByName"

4. Expected results, and actual results in other browsers:

   Displays three alerts saying:
      "before getElementsByName"
      "after getElementsByName"
      "The 1. radio button is checked"
getElementsByName is only supported on HTMLDocument objects.  The document in http://www.exampleonly.com/getelementsbyname.html (and likewise for http://www.exampleonly.com/getelementsbyname2.html) is not an HTMLDocument.  Whether this is because of the XSL involved or because the page is served with the type text/xml, not application/xhtml+xml, I don't know.
(Reporter)

Comment 5

7 years ago
It turns out that other browsers such as Opera must coerce the document object to an HTMLDocument whether it should be or not, as shown by the now modified alert in http://www.ExampleOnly.com/getelementsbyname2.html. The resolution is to add an xsl:output element to the "Fix for IE" code from W3C's FAQs (http://www.w3.org/MarkUp/2004/xhtml-faq#ie), as shown in:

    http://www.ExampleOnly.com/getelementsbyname3.html

And now the "fruit" example works!

Thank you very much, Boris.
You need to log in before you can comment on or make changes to this bug.