Closed
Bug 849551
Opened 12 years ago
Closed 12 years ago
JavaScript runs else block after if block
Categories
(Core :: JavaScript Engine, defect)
Tracking
()
RESOLVED
INVALID
People
(Reporter: ken_g6151, Unassigned)
References
Details
Attachments
(1 file)
161.08 KB,
application/octet-stream
|
Details |
User Agent: Mozilla/5.0 (X11; Linux x86_64; rv:20.0) Gecko/20100101 Firefox/20.0
Build ID: 20130307075451
Steps to reproduce:
1. Start with a clean Firefox 19 or 20 profile, in Linux Mint Debian Edition XFCE, or Windows XP.
2. Install the Firebug add-on, mainly because I don't see an easier way to inject JavaScript into a page.
3. Visit http://www.anandtech.com/bench/Product/80?vs=76
4. Bring up Firebug (F12).
5. Enable the Console tab.
6. Expand the Command Editor to allow entry of multiple lines of JavaScript.
7. Copy and paste the contents of js_bug.txt into the Command Editor.
8. Run the JavaScript.
Actual results:
All the benchmarks (except the first) turned blue. See js_bug_firefox.png. If line 49 of the script is commented out, some lines turn orange. If I instead place a breakpoint at line 47 in Firebug (also using Greasemonkey), I can see that execution goes from line 47 to line 49, jumping from the if block to the else block.
Expected results:
Some benchmarks should have turned blue; some should have turned orange. See js_bug_chrome.png for the correct result. I can perform almost the same steps in Chromium, and it produces this correct result.
Further research indicates this may not be directly related to the if. I can take the else code, move it above the if, and the same result still happens. When I do that, in Firebug debugging, the instruction pointer jumps from the end of the if to the line before the if. It's like the loop, or part of the loop, is being run twice or something. Very strange.
If I add another if around the assignment that was in the "else", "if(row.children[0].style.background == '')", the visual result becomes correct. But that shouldn't be necessary.
Comment 2•12 years ago
|
||
The problem is that you're iterating over the `rows` array with a for (.. in ..) loop. That loop type loops over numeric indices as well as named properties of a list. Apparently, Firefox's `children` HTMLCollection (which Node.children returns) contains all children as indexed items as well as named properties.
Because of that, you are effectively processing each element twice.
You get the expected result if you change your loop header to read
for (var j = 0; j < rows.length; j++) {
var row = rows[j];
Status: UNCONFIRMED → RESOLVED
Closed: 12 years ago
Resolution: --- → INVALID
You need to log in
before you can comment on or make changes to this bug.
Description
•