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.
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.
Share Improve this question edited Mar 17, 2011 at 17:23 KongMD asked Mar 15, 2011 at 14:27 KongMDKongMD 1811 gold badge4 silver badges14 bronze badges 5- On which line is the error triggered? – Damp Commented Mar 15, 2011 at 14:52
- It's about 6 lines from the end, actually. I mented "ERROR ON THIS LINE" to try and make it stand out. I included the other code just for context. – KongMD Commented Mar 15, 2011 at 15:05
- I don't see where i is defined anywhere in the code you posted, so presumably the value of i is where the problem is. – Mark Bessey Commented Mar 15, 2011 at 15:10
- does it say what line the error occurs on ? that might give us all a better clue – mcgrailm Commented Mar 15, 2011 at 15:14
- Anyone have any ideas? I edited the OP to make the code easier to see at-a-glance. – KongMD Commented Mar 16, 2011 at 10:54
2 Answers
Reset to default 5Strictly working off of the currently posted code, here are the issues I see:
The posted code looks inplete. 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 ment 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 ment 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 plete 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 bees:
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.