deleteRow causes Netscape 7 to crash - why ?

D

Dom

Hi,

I am trying to get a dynamic table going. You click a button to add a
row. In the newly created row a button to delete the row is placed
(try the code example below). In Netscape, pressing the delete row
button crashes the browser. Why ? What is the correct way to do this ?

I am stumped why this following code works in IE and Opera but crashes
Netscape 7 (ie makes it disappear and try to send a bug report etc).

Any help would be much appreciated.

Copy and save the following code example and try running in Netscape 7
(or Mozilla 1.4) . It works fine in IE 6 and Opera 7. I've tried all
sorts of tweaking around but to no avail.

I need some expert help. Perhaps suggest the correct or other better
way of implementing this.


Here's the code:

<html><head>

<script language="javascript">

function createTableRow(oTable)
{
var thetable = document.all ? document.all[oTable] :
document.getElementById(oTable); // get table object handle
var nrows=thetable.rows.length; // get current tablesize
var row = thetable.insertRow(nrows); // create a new row object
var cell ;

cell = row.insertCell(0); cell.innerHTML="row"+nrows; // create a new
cell in the new row

// this will crash my netscape 7.2 browser but works fine in Opera
and IE
cell = row.insertCell(1); cell.innerHTML="<button
onclick='deleteTableRow(this.parentNode.parentNode.rowIndex)'>Delete</button>";

// This will behave strangely in that it will remove ALL rows from
the table - why ??? again, it works in Opera and IE.
//cell = row.insertCell(1); cell.innerHTML="<button
onclick='deleteTableRow(0)'>Delete</button>";
}

function deleteTableRow(nth)
{
var thetable = document.all ? document.all['tbl'] :
document.getElementById('tbl'); // get the tbl table object handle
thetable.deleteRow(nth); // delete the nth row from the table ####
this crashes Netscape 7 and 6 - WHY ????
}
</script>


<body bgcolor=white>
<form name="form1" method="post" action=""
enctype="multipart/form-data" >
<table border=1 width="232">
<!--col width=150><col width=150><col width=80-->
<tr><th>Name</th><th>Qty</th></tr>
</table>

<table id='tbl' border=1>
<col width=150><col width=150><col width=80>
<!-- TABLE for the order items -->
</table>

<input TYPE="button" VALUE="Add Item"
onclick="createTableRow('tbl')">
<input type="submit" name="Submit" value="Submit" >

</form></body></html>
 
D

DU

Dom said:
Hi,

I am trying to get a dynamic table going. You click a button to add a
row. In the newly created row a button to delete the row is placed
(try the code example below). In Netscape, pressing the delete row
button crashes the browser. Why ? What is the correct way to do this ?

I am stumped why this following code works in IE and Opera but crashes
Netscape 7 (ie makes it disappear and try to send a bug report etc).

Any help would be much appreciated.

Copy and save the following code example and try running in Netscape 7
(or Mozilla 1.4) . It works fine in IE 6 and Opera 7. I've tried all
sorts of tweaking around but to no avail.

I need some expert help. Perhaps suggest the correct or other better
way of implementing this.


Here's the code:

<html><head>

<script language="javascript">

function createTableRow(oTable)
{
var thetable = document.all ? document.all[oTable] :
document.getElementById(oTable); // get table object handle

Since the rest of the function can not be executed by MSIE 4, then there
is no point to branch the code in the above instruction.
var nrows=thetable.rows.length; // get current tablesize
var row = thetable.insertRow(nrows); // create a new row object
var cell ;

cell = row.insertCell(0); cell.innerHTML="row"+nrows; // create a new
cell in the new row

// this will crash my netscape 7.2 browser

we're still at NS 7.1 :)


but works fine in Opera
and IE
cell = row.insertCell(1); cell.innerHTML="<button
onclick='deleteTableRow(this.parentNode.parentNode.rowIndex)'>Delete</button>";

I tried a few modifications of the above instruction and nevertheless,
NS 7.1 always crashed. You might have found an old - but rare - crash
bug here ... since the code works and does not crash in Opera 7.2 and
Mozilla 1.5RC2
// This will behave strangely in that it will remove ALL rows from
the table - why ??? again, it works in Opera and IE.
//cell = row.insertCell(1); cell.innerHTML="<button
onclick='deleteTableRow(0)'>Delete</button>";
}

function deleteTableRow(nth)
{
var thetable = document.all ? document.all['tbl'] :
document.getElementById('tbl'); // get the tbl table object handle
thetable.deleteRow(nth); // delete the nth row from the table ####
this crashes Netscape 7 and 6 - WHY ????
}

This deleteTableRow function could be safely removed and replaced entirely.
</script>


<body bgcolor=white>
<form name="form1" method="post" action=""
enctype="multipart/form-data" >
<table border=1 width="232">
<!--col width=150><col width=150><col width=80-->
<tr><th>Name</th><th>Qty</th></tr>
</table>

<table id='tbl' border=1>
<col width=150><col width=150><col width=80>
<!-- TABLE for the order items -->
</table>

<input TYPE="button" VALUE="Add Item"
onclick="createTableRow('tbl')">
<input type="submit" name="Submit" value="Submit" >

</form></body></html>


Your code could be optimized and be made much more compact at several
places. Your code works in Mozilla 1.5RC2 but it crashes in NS 7.1.
I would avoid resorting to innerHTML everywhere all the time and only
use DOM 2 methods. Here, with DHTML modifying the page, such coding
policy is even more true and recommendable.

In your code, your table "tb1" has 3 columns but each new row only has 2
cells (without any column span).
Despite several efforts, I could not come up with a solution which would
work in NS 7.1.

DU
--
Javascript and Browser bugs:
http://www10.brinkster.com/doctorunclear/
- Resources, help and tips for Netscape 7.x users and Composer
- Interactive demos on Popup windows, music (audio/midi) in Netscape 7.x
http://www10.brinkster.com/doctorunclear/Netscape7/Netscape7Section.html
 
R

Richard Cornford

I tried a few modifications of the above instruction and
nevertheless, NS 7.1 always crashed. You might have found
an old - but rare - crash bug here ... since the code works
and does not crash in Opera 7.2 and Mozilla 1.5RC2
<snip>

I could not get Netscape 7.02 to crash but I did get Mozilla 1.3 to
crash so I tested in that until it worked (at my first alteration).

The button element that is being written into the HTML has no type
attribute so the HTML 4 specification requires it to default to
type="submit". Obviously submitting a form while simultaneously deleting
it's submit button is a bit much for Mozilla 1.3 so it crashes. Adding a
type="button" attribute solved the problem.

(Another example of how understanding what represents valid HTML and its
meaning would have avoided JavaScript errors ;-)

Richard.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,772
Messages
2,569,593
Members
45,111
Latest member
KetoBurn
Top