Bug 49501 - console.log(document.getElementsByTagName(..)) outputs wrong value when DOM is later modified
Summary: console.log(document.getElementsByTagName(..)) outputs wrong value when DOM i...
Status: RESOLVED DUPLICATE of bug 35801
Alias: None
Product: WebKit
Classification: Unclassified
Component: Web Inspector (Deprecated) (show other bugs)
Version: 528+ (Nightly build)
Hardware: PC OS X 10.5
: P2 Normal
Assignee: Nobody
Depends on:
Reported: 2010-11-13 15:38 PST by Evan Martin
Modified: 2012-08-02 07:47 PDT (History)
5 users (show)

See Also:

html file showing problem (487 bytes, text/html)
2010-11-13 15:38 PST, Evan Martin
no flags Details
better demo (585 bytes, text/html)
2010-11-13 15:49 PST, Evan Martin
no flags Details

Note You need to log in before you can comment on or make changes to this bug.
Description Evan Martin 2010-11-13 15:38:25 PST
See attached test case.

Basically, getElementsByTagName() returns a value like you've already executed the code that *follows* it.  If you remove the later code, the earlier code behaves as expected.

Google Chrome 9.0.576.0 (Official Build 65344) dev
WebKit	534.12
V8	2.5.4

Works in Firefox 3.6.12.
Comment 1 Evan Martin 2010-11-13 15:38:48 PST
Created attachment 73834 [details]
html file showing problem
Comment 2 Evan Martin 2010-11-13 15:39:35 PST
(Note that this only dumps to the JS console, so you need Firebug installed on Firefox to test it.)
Comment 3 Evan Martin 2010-11-13 15:49:00 PST
Created attachment 73836 [details]
better demo

Here's a better example, where we flip a coin to decide whether to remove the node or not.  Load this, bring up the console, repeatedly hit f5 and boggle at the first line of output changing.

I guess something is printing the actual value of getElementsByTagName too late -- we always seem to successfully get the first node, whether or not the console.log thinks it exists.
Comment 4 Adam Barth 2010-11-13 16:10:58 PST
I think getElementsByTagName is returning a live node list.  You're then modifying the node list.  The question is when we read the value of the node list to print.  With the inspector using an async API, I can see why this might occur after later than you expect.
Comment 5 Sam Weinig 2010-11-14 11:57:37 PST
This seems like an inspector bug and not a DOM bug.  If you log document.getElementsByTagName('ul').length instead of just document.getElementsByTagName('ul'), you will see that the first log has two elements and the second log as one element.

Changing component to Web Inspector.
Comment 6 Evan Martin 2010-11-15 10:35:52 PST
With another day's perspective on my code.

=> prints the list with only the second one

var x = document.getElementsByTagName('ul')[0];
=> x successfully is the first (missing) item.

So it's clear that document.getElementsByTagName is returning the right thing, and it's just the console.log that is confusing.  I'll retitle the bug to something less mysterious.
Comment 7 Paul Irish 2012-08-02 07:47:01 PDT
A bit later, but thanks for the great repro Evan. (and folks for the help)
Duping here.

Also downstream ticket is at http://crbug.com/50316

*** This bug has been marked as a duplicate of bug 35801 ***