7

I need to understand when does a body's onload gets called

I read in w3school that onload=Script to be run when a document load what does this mean?

Does it mean that

  1. the html/jsp page gets loaded before rendering any elements in the body like any table or jsp scriplets eg: <%= request.getParameter("NAME") %>
  2. Or is it after the page/body is rendered?

for example: I have a bunch of params (<%= request.getParameter("NAME") %>,...) so in order to use them i'll place them in some hidden form item & then on body load can I use them?

  • 5
    w3schools is a bad resource: http://w3fools.com. While it might not matter in this case, better use a proper reference such as mozilla's [MDN](https://developer.mozilla.org/) – ThiefMaster May 08 '12 at 07:47
  • @ThiefMaster: My work is mostly concerned to Internet Explorer 8, is it still fine to refer [MDN](https://developer.mozilla.org/)? –  May 08 '12 at 08:24
  • 1
    Usually it mentions if other browsers have important differences. But to be sure I'd check MSDN in this case since that's the official resource for IE. – ThiefMaster May 08 '12 at 09:17

4 Answers4

11

Unlike w3schools, MDN explains when the event is fired:

The load event fires at the end of the document loading process. At this point, all of the objects in the document are in the DOM, and all the images and sub-frames have finished loading.

So the DOM tree is ready and all elements have been loaded.

ThiefMaster
  • 310,957
  • 84
  • 592
  • 636
  • To @Sjoerd & ThiefMaster: Sjoerd says `Document.onload may be triggered before the page is rendered or images are loaded.` and you say `all of the objects in the document are in the DOM, and all the images and sub-frames have finished loading.` which one is correct? –  May 08 '12 at 08:27
  • @Ricky — The answer that actually quoted the resource it linked to is the correct one. :) – Quentin May 08 '12 at 09:43
  • What about objects added after the page starts loading? For instance, suppose in the head tag I have a script which creates a new script tag with an external url (which it computes) and adds it to the head. Am I guaranteed that script will always be loaded before onload fires like I am if the script was hard coded? – Michael Oct 11 '17 at 22:38
2

The onLoad event is fired after the successful completion of the dom tree, e.g. the successful loading of all the elements inside the body.

rdurand
  • 7,342
  • 3
  • 39
  • 72
0

Onload executes when DOM fully loaded.This means it is executed after end of your page. This is useful when you want to do some task when document is fully loaed.

You can do this in many ways but few ways is here

<body onload="YOUR ACTION">

is similar to

<script type="text/javascript>
window.onload=function(){
   //YOUR ACTION  
}
</script>
  • 1
    It doesn't fire as soon as the DOM is loaded. It waits for a number of other things (e.g. images to load). – Quentin May 08 '12 at 09:42
-1

The onload event is triggered when the DOM tree is ready. This means that all elements on your page are available to the script. Document.onload may be triggered before the page is rendered or images are loaded.

Sjoerd
  • 74,049
  • 16
  • 131
  • 175
  • The window.onload event (the one that you linked to) fires after the images frames etc have loaded. – Salman A May 08 '12 at 07:52