Vanilla JS equivalent of $(document).ready()

Javascript $(document).ready()

Posted on November 14, 2013

I’ve seen this asked a few times so thought I’d post it.

The closest you’ll get to $(document).ready() using Vanilla Javascript is

window.onload = function() {
  doSomething());
};

Unfortunately this isn’t a direct equivalent as $(document).ready() fires before window.onload. The onload function waits for all external assets including images to load before firing, whereas $(document).ready() will fire once the DOM tree has been completed and is ready to be manipulated.