views:

82

answers:

4

hi, everyone,

I want to ask a question about the javascript "onload". I am writing the JSP page and I use the <%@ include file ="body.jsp". and I use the following statement in the body.jsp.

<table onload="function()">

to load a javascript function, but it doesn't have any action of the page. Is the "onload" can only be used in the body tag? Thank you.

+4  A: 

Onload can only be used for <body>, <img>, <script>, <iframe> tags, because it tells you when an external resource (image, script, frame) or the whole page (body) has been loaded

galambalazs
+1  A: 

"onLoad" may be used on body- and frameset-tags. To see some action you may use:

<body onload="function(){alert('This is an action!')}">
Thariama
+2  A: 

As the other guys already stated the onLoad event will not fire on a table. What you can do ist attaching the onLoad-handler to the body element (which will then fire, when the page is loaded) and manipulate the table by for example assigning an id to the table.

<body onload="function() { var table = document.getElementById("table-id"); ... }">
    <table id="table-id"></table>
</body>

Are you using some javascript framework?

philgiese
+1  A: 

Why not just include it via a <script tag>?

Inside your .jsp file

<script>
    window.onload = function() {
        alert("Hello Mars!"); // so unoriginal
    }
    // or to execute some function
    window.onload = myFunction();
</script>
Marko