+4  A: 

You probably want to use something like jQuery, which makes JS programming easier.

Something like:

$(document).ready(function(){
   // Your code here
});

Would seem to do what you are after.

danros
And if he wants to see how jQuery does it in a portable way, jQuery's source is there for the inspection :-)
Thomas L Holaday
And in case it isn't clear, if `ready()` is called *after* document load, the passed function is run immediately.
Ben Blank
+1  A: 

Use your favorite Javascript library to define what you want to do as an event handler and then bind it to the window.load event.

Here it is in bare javascript:

<html>
<head>
</head>
<body>
<script>
    window.onload = function()
    {
        var element = document.getElementById("hello");
        alert(element.innerHTML);
    }
</script>
    <span id="hello">Hello World.</span>
</body>
</html>

Note that while this might work fine for an example, every time you bind to window.onload, you overwrite it, which can be a pain if you're generating multiple methods inside server controls or something similar. Every single external javascript library has methods that fix that problem.

Dan Monego
+2  A: 

If you actually want this code to run at load, not at domready (ie you need the images to be loaded as well), then unfortunately the ready function doesn't do it for you. I generally just do something like this:

Include in document javascript (ie always called before onload fired):

var pageisloaded=0;
window.addEvent('load',function(){
 pageisloaded=1;
});

Then your code:

if (pageisloaded) {
 DoStuffFunction();
} else {
 window.addEvent('load',DoStuffFunction);
}

(Or the equivalent in your framework of preference.) I use this code to do precaching of javascript and images for future pages. Since the stuff I'm getting isn't used for this page at all, I don't want it to take precedence over the speedy download of images.

There may be a better way, but I've yet to find it.

Nathan
A: 

http://www.javascriptkit.com/dhtmltutors/domready.shtml there it's explained how to do it without any libraries at all

+2  A: 

No need for a library. jQuery used this script for a while, btw.

http://dean.edwards.name/weblog/2006/06/again/

// Dean Edwards/Matthias Miller/John Resig

function init() {
  // quit if this function has already been called
  if (arguments.callee.done) return;

  // flag this function so we don't do the same thing twice
  arguments.callee.done = true;

  // kill the timer
  if (_timer) clearInterval(_timer);

  // do stuff
};

/* for Mozilla/Opera9 */
if (document.addEventListener) {
  document.addEventListener("DOMContentLoaded", init, false);
}

/* for Internet Explorer */
/*@cc_on @*/
/*@if (@_win32)
  document.write("<script id=__ie_onload defer src=javascript:void(0)><\/script>");
  var script = document.getElementById("__ie_onload");
  script.onreadystatechange = function() {
    if (this.readyState == "complete") {
      init(); // call the onload handler
    }
  };
/*@end @*/

/* for Safari */
if (/WebKit/i.test(navigator.userAgent)) { // sniff
  var _timer = setInterval(function() {
    if (/loaded|complete/.test(document.readyState)) {
      init(); // call the onload handler
    }
  }, 10);
}

/* for other browsers */
window.onload = init;
galambalazs