0

Summary: Following body onload works when a link to another script using <script> is included in the head tag but not while the same <script> is included just before the end of body

<body onload="console.log('body loaded');">

To Reproduce:

Load Complete code in Fiddle

When we set the Javascript Loadtype to No Wrap - In <head>, we see the following logs in the console:

DOM Loaded
window loaded
body loaded

But when we set it to No Wrap - In <body>, we see the following logs in the console:

DOM Loaded
window loaded

Why the difference?

Keerthi Kumar P
  • 1,554
  • 9
  • 14

1 Answers1

0

Setting it in body tag needs to wait for all the resources to be rendered in page such as images, styles, scripts etc.

When you put it inside head tag, no need to wait for the resources to be rendered.

It's like between window.on('load') and document.ready in jquery.

aldrien.h
  • 3,437
  • 2
  • 30
  • 52