<Html>
<Head>
<Title> instance code-use JavaScript and DOM to create an HTML table </title>
<Script>
Function start (){
// Obtain the body tag
Var mybody = document. getElementsByTagName ("body") [0];
// Create a <table> element and a <tbody> element
Table1 = document. createElement ("table ");
Mytablebody = document. createElement ("tbody ");
// Create all cells
MyArray = new Array ('this', 'is ', 'A', 'test ');
For (var j = 0; j <4; j ++ ){
// Create a <tr> element
Mycurrent_row = document. createElement ("tr ");
For (var I = 0; I <4; I ++ ){
// Create a <td> element-column
Mycurrent_cell = document. createElement ("td ");
// Create a text node
If (j = 0 ){
Currenttext = document. createTextNode (myArray [I]);
} Else if (I = 0 ){
Currenttext = document. createTextNode (myArray [j]);
} Else {currenttext = document. createTextNode ();
}
Mycurrent_cell.appendChild (currenttext );
// Add the column <td> to the row <tr>
Mycurrent_row.appendChild (mycurrent_cell );
}
// Add the row <tr> to <tbody>
Mytablebody. appendChild (mycurrent_row );
}
// Add <tbody> to <table>
Table1.appendChild (mytablebody );
// Add <table> to <body>
Mybody. appendChild (table1 );
Table1.setAttribute ("border", "2 ");
Table1.setAttribute ("cellspacing", "3 ");
Table1.setAttribute ("cellpadding", "2 ");
}
</Script>
</Head>
<Body onload = "start ()">
</Body>
</Html>