JavaScript: load event on body

The load event can be even attached to the body element of an HTML page. The problem with this event is that if you insert your JavaScript code just inside the body element, browsers react differently. For example, Safari, Opera and Chrome will execute the code attached to this event, whereas Firefox and IE won't do nothing. This demo page shows the problem: you should see an alert with the string Loaded when you first load the page. As you can see, not all browsers display the alert. Everything is due to evaluation of your code during HTML parsing. In fact, browsers parse the document, find your script inside the body element and then try to execute it. In other words, some browsers think that when everything has been loaded, there's nothing to execute. That could be a problem sometimes.

One thought on “JavaScript: load event on body”

Leave a Reply

Note: Only a member of this blog may post a comment.