While using Firefox, I keep getting the error described in the subject line for this block of code:
for(var i = 0; i < tables.length; i++)
{
var j = rows.length - 1;
while(j--)
{
if(hideDP && tables[i].innerHTML.indexOf(">D<") != -1)
{
if(!platTag && !soulSilverTag && pearlTag)
{
tables[i].deleteRow(j);//ERROR IS ON THIS LINE
}
}
}//end while loop (rows)
}//end for loop (tables)
I suspect that this error is because I'm somewhat 开发者_高级运维new to making reverse loops, but I specifically made a reverse loop in this instance because it made deleting rows from a table easier. Note also that j is something like 24 and i is 0, so they're non-negative. Could someone shed some light on this for me?
EDIT : The full code can be found here.
Strictly working off of the currently posted code, here are the issues I see:
The posted code looks incomplete. Where is
rows
being initialized? This could cause the stated error.Given
while(j--)
; Thevar j = rows.length - 1;
line is incorrect. That is, unless you know that the last row will never need deleting. But if that is the case, then comment the code to make it clear.For example, if there were 4 rows, the current code initializes
j
to 3, but because of the location of the--
operator, the inside of the loop sees: 2, 1, 0. For the code as shown, usevar j = rows.length;
or add a comment to show that the logic is deliberate.The 2
if()
statements do not depend onj
at all! (At least as the code is posted here.) If this is true, then move the conditionals outside of thej
loop.Consider posting the full, unedited, code. Or linking to it on a site like Pastebin.
Update for full script, now that it's been linked to:
Scanning the complete code, it looks like tables[i].deleteRow(j);
can be called multiple times for the same row.
The easy solution, that should be done anyway, is to add a continue
statement after each row delete.
For extra credit, reanalyze and simplify the flag and if
logic too. :)
Update for target page, now that it's been linked to:
Examining the target page, the tables being looped by this script contain nested tables.
That throws off the row count in this line:
var rows = tables[i].getElementsByTagName("tr");
Sometimes making it seem like table[i] has more rows than it really directly owns.
Solution, use the built in rows array; so the line becomes:
var rows = tables[i].rows;
~~~~
While examining the script relative to the target page, a few other issues seemed apparent:
It's not best to loop through all tables. Target just the ones you need. So this:
tables = document.getElementsByTagName("table");
Should be changed to:
var tables = document.querySelectorAll ("div.KonaBody > table.roundy");
...which will select just the 4 payload tables, and not their subtables or the other tables scattered about.
By fine-tuning the initial table selection, the following, problamatic, test is not needed:
if(tables[i].getAttribute("style").indexOf("border: 3px solid") != -1)
- Missing
var
in front of themajorSections
initialization.
That error will also be generated if j is >= to the amount of rows in the table, but I'm not seeing the exact problem.
精彩评论