How to add a page onload event

Tagged:  

How to make a function execute when the page is loaded without putting it in the body tag with onload, but somewhere else in the page.
<script type="text/javascript">
       
      var loadfunction = function() {
            alert("Hello World 1");
      }
 
      function addEventSimple(obj,evt,fn) {
         if (obj.addEventListener)
                obj.addEventListener(evt,fn,false);
         else if (obj.attachEvent)
                obj.attachEvent('on'+evt,fn);
      }    
          addEventSimple(window,'load',loadfunction);
     
</script>