Pure Javascript DOM Ready

Yet another a pure Javascript DOM Ready option. The concept was gathered from many options that were considered (in particular from Stackoverflow), which resulted in the below code:

(function(f){var t=setInterval(function(){if(document.readyState=="complete"){clearInterval(t);f();}},9);})();

Features:

  • Javascript library independant.
  • Doesn’t pollute global scope.
  • That’s it. 🙂

Usage:

var f = function(){
  alert('Hello, World!');
};
(function(f){var t=setInterval(function(){if(document.readyState=="complete"){clearInterval(t);f();}},9);})(f);

Or, if you’re into one-liners like me:

(function(f){var t=setInterval(function(){if(document.readyState=="complete"){clearInterval(t);f();}},9);})(function(){alert('Hello, World!');});

Enjoy!