Click to See Complete Forum and Search --> : Javascript error


nazgul27
September 5th, 2007, 09:34 AM
Hi guys,

I have a table with 50 rows on my page. I need to update the table on runtime by deleting the last row (the oldest) and adding a new one.

Here is the code to delete the last row:



<script...>

var t = document.all ("Table"); // get a ref to our table
t.deleteRow (t.rows.length -1); // delete last row

</script>



everything is fine but sometimes I get a script error:
rows.length is null or not an object.

what could cause that error? There are rows in that table at the moment I get this error.

Thanks

PeejAvery
September 5th, 2007, 02:59 PM
Sounds to me like you need to make sure your code fires after the document has completed loading. Place an onload event in the <body> tag.

nazgul27
September 5th, 2007, 04:01 PM
this error happens in an AJAX request handler. IT happens way after the page is loaded. In other words I open the page and an AJAX requests comes out every three seconds. When the request is complete and processed this function is called to process the request's results. For example I left the page opened and this error happened three hours after I had opened the page

PeejAvery
September 5th, 2007, 04:03 PM
Then you need to supply us with more code. We cannot simulate this issue with the current code you have provided.

nazgul27
September 5th, 2007, 04:20 PM
This is the AJAX response handler:



function OnUpdateComplete(results)
{
if (results.ar.length)
{
var t = document.getElementById("Table");

for (i = results.ar.length - 1; i >= 0; --i)
{
// delete last row
if (t.rows.length != null)
t.deleteRow (t.rows.length-1);

// insert a row at the top of the table
var newRow = t.insertRow(0);
newRow.id = results.ar[i].id;

// insert the first cell
var oCell = newRow.insertCell();
var text = "<span style=font-family:Verdana;font-size:10pt;font-weight:bold;>";
text += "test";
text += "</span>";

oCell.innerHTML = text;
oCell.vAlign = "top";

// second cell
oCell = newRow.insertCell();

var text = getCellText (results);

oCell.innerHTML = text;
}
// reset a timer for AJAX request
setTimeout("AddNewHeadlineToGrid()", 3000);
}


This is the HTML code for the table element


<asp:Table ID="Table" runat="server" BorderWidth="0px" CellPadding="2" CellSpacing="0" Width="562px">
</asp:Table>
</table>


I don't think you will be able to reproduce it. As I said the page should be opened for some quiet time. I just want to make sure that there is nothing with the current code and it conforms to all HTML standards.

Thanks

PeejAvery
September 5th, 2007, 06:12 PM
Well, I don't know why this works. It should be i--.
for (i = results.ar.length - 1; i >= 0; --i)