Re: load event for dynamic iframes - firing when changing the frame's location

On 9/4/07, Michael A. Puls II <shadow2531@gmail.com> wrote:
> When you create an iframe with innerHTML or createElement and the
> iframe has an onload attribute set (or .onload = function or
> addEventListener), when the iframe is added to the document, the js in
> the onload attribute is executed in FF, Safari and Opera.
>
> Now, after the iframe is added to the document, if you change the
> location (via .src or .location or by submitting a form that targets
> the iframe, whether the js in the iframe's onload attribute fires
> again depends on what browser you're using.
>
> In FF and Safari, it is not fired again. In Opera it is.
>
> So, basically, FF and Safari do not fire a load event for the location
> change of dynamic iframes. Opera does.

Firefox does fire the load event after the location change.  For
example, modify your test case to change this line:
iframe.setAttribute("onload", "alert('load event for dynamic iframe fired)");
to this:
iframe.setAttribute("onload", "alert('load event for dynamic iframe
fired: ' + this.src)");

You'll see that Firefox is only showing one alert, and it's showing
the alert on the load event AFTER changing the .src attribute.  The
first load event never gets fired because your test case changes the
.src attribute before it gets a chance.

If you modify the test case to separate these events [1], you'll see
that they both get fired.


>
> Now, for changing the location of parsed iframes, Safari does the same
> as it does with dynamic ones and doesn't fire.  Firefox however, does
> fire and so does Opera.

Safari may have had the same issue here - you changed the iframe's src
before it was ever initially loaded.  Of course, then we get into the
question of whether window.onload should have been fired before the
iframe had initially loaded.  I haven't looked at that part of the
draft recently to see if it says anything about that.

Either way, we agree that the iframe.onload event should be fired when
iframe's location is changed (and the new resource has loaded) no
matter what, right?

> --
> Michael
>
>

[1] <!DOCTYPE HTML>
<title>dynamic iframe</title>
<script>
window.onload = function() {
iframe = document.createElement("iframe");
iframe.setAttribute("onload", "alert('load event for dynamic iframe
fired: ' + this.src)");
document.body.appendChild(iframe);
};
</script>
<style>iframe {	width: 0; height: 0px; }
</style>
<p><button type="button" onclick="iframe.src = './';">Change Iframe's
@src</button></p>
-- 
Jon Barnett

Received on Wednesday, 5 September 2007 13:49:49 UTC