Online Shopping : Computers : Programming : Languages : JavaScript

+ Search
Add Entry AlertManage Folder Edit Entry Add page to
Did You Find This Entry Useful?

22 of 27 people (81%) answered Yes
Recently 7 of 10 people (70%) answered Yes


Why can't I have an onLoad event in an iFrame tag on IE 5.0?

Apr 6th, 2008 19:29
ha mo, Dave Clark, Steven Armstrong, Jean-Bernard Valentaten, Ken Swift,

Both iframe and frameset do only define where to set a frame in the
document. While frameset will generate a whole frameset and can thus 
be used within a <body>-scope, iframe only declares that there should 
a frame  at a certain position within the document and thus within a
But both do load external documents!!
Documents must (they should) constis of a <head>-area and a <body>-
both enclosed within a <html>-scope.
Now to answer your question, ask yourself "Does a <html>-tag have an
onLoad eventhandler??"
The answer is no, thus the answer to your question is:
The onLoad eventhandler must be used with the body-tag of the document
you wish to load within your iframe, since the iframe itself is not
loaded (see definition above).
It is correct that you can't do this with static html, but it can be 
done with JavaScript using something like:
onload = function() {
 oIframe = document.getElementById('theiframesid');
 oIframe.attachEvent('onload', fnFrameLoadHandler );
 oIframe.contentWindow.document.location = 'newiframepage.html';
function fnFrameLoadHandler() {
// do something here using the this-keyword to refer to the iframe 
another cool thing about it is that you can register multiple onload 
handlers on the same iframe.
     Steven pointed this out, but didn't explain...  In point of fact, 
the "onload" event you specify in the BODY tag actually attaches to 
the "window" object -- not the "document" object.  The FRAME tag and 
the IFRAME tag are both rendered as the "window" object.  Thus, though 
you can't specify an "onload" attribute in the HTML for the FRAME or 
the IFRAME tag, you can certainly use JavaScript to attach an event 
handler to the FRAME or IFRAME's DHTML object using their "onload" 
event.  For example:
self.frames['iframeName'].onload = handler;
Take care,
Dave Clark