title: How to call function without onload
body: <p>I was trying to figure out a way to dynamically write to a table in the body from the head and came across this sample code. The problem is that it calls the function using 'onload'. I need to call the function from a loop in my code to write to the table.</p>
  
  <pre><code>&lt;!DOCTYPE HTML&gt;
  &lt;html&gt;
  &lt;head&gt;
  &lt;title&gt;Untitled Document&lt;/title&gt;
  
  &lt;style type="text/css"&gt;
  .mytable {
  border:1px solid #000000;
  border-collapse:collapse;
  width:200px;
  }
  .mytable td{
  background:#cccccc;
  border:1px solid #000000;
  }
  &lt;/style&gt;
  
  &lt;script type="text/javascript"&gt;
  onload=function(){
  var nrCols=2;
  var maxRows=4;
  var nrRows=maxRows+1;
  while(nrRows&gt;maxRows){
  nrRows=Number(prompt('How many rows? Maximum '+maxRows+' allowed.',''));
  }
  var root=document.getElementById('mydiv');
  var tab=document.createElement('table');
  tab.className="mytable";
  var tbo=document.createElement('tbody');
  var row, cell;
  for(var i=0;i&lt;nrRows;i++){
      row=document.createElement('tr');
      for(var j=0;j&lt;nrCols;j++){
          cell=document.createElement('td');
          cell.appendChild(document.createTextNode(i+' '+j))
          row.appendChild(cell);
      }
      tbo.appendChild(row);
  }
  tab.appendChild(tbo);
  root.appendChild(tab);
  }
  &lt;/script&gt;
  &lt;/head&gt;
  &lt;body&gt;
  &lt;div id="mydiv"&gt;&lt;/div&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>I tried to name the function and get rid of the onload and call the function from the script in the head but that doesn't work.</p>