Javascript onload in HTML

Viewed 102532

I want to ask a question about the Javascript’s onload.

I’m writing a JSP page with the code <%@ include file ="body.jsp". The included body.jsp contains:

<table onload="function()">

This should load the javascript function, but it doesn't appear to have any effect on the page. Is onload only usable on the body tag?

6 Answers

You can throw a <script> tag right after your table with code. Once it gets to the script tag it would mean that the DOM for the table element above it has been loaded and can now be accessed in your script below it.

Note: The following below isn't applicable to the question but rather the other answers being given.

I recommend using the addEventListener function in javascript for adding the event. This makes sure that you are not overwriting or going to be overwritten by anyone else wanting to listen to the event.

Example

var iframe = document.getElementsByTagName('iframe')[0];
iframe.addEventListener('load', function(event){ console.log("iframe Loaded", event); })
Related