JS In the Browser

Include in HTML page with

Running JS after DOM loads

defer like above

window.onload = function() {
  init();
  doSomethingElse();
};

window.onload

  • By default, it is fired when the entire page loads, including its content (images, css, scripts, etc.)

  • In some browsers it now takes over the role of document.onload and fires when the DOM is ready as well.

document.onload

  • It is called when the DOM is ready which can be prior to images and other external content is loaded.

Last updated