JavaScript Method For Launching A Callback When The Document Is Ready

Posted at

it is a pretty much what jQuery does, slightly improved (and without using jQuery).
function document_ready(callback){
  function completed(){
    document.removeEventListener("DOMContentLoaded", completed, false);
    self.removeEventListener("load", completed, false);
    self.setTimeout(function(){ callback(); },10);  //timeout helps to execute in a semi-async way.
  if("loading" !== document.readyState){    //interactive or complete
  else{                                     //loading
    document.addEventListener("DOMContentLoaded", completed, false);  //Use the handy event callback.
    self.addEventListener("load", completed, false);                  //A fallback to window.onload, that will always work.