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

On 9/5/07, Jon Barnett <jonbarnett@gmail.com> wrote:
> On 9/4/07, Michael A. Puls II <shadow2531@gmail.com> wrote:
> > 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.

Thanks. I see now.

If a src change happens fast enough so that it happens before the
resource finishes loading, it will prevent the first load event.

In the case of my example and some others, Opera appends the iframe to
the document and loads the resource so fast that the src change
doesn't even have a chance to interfere with the first load event.

In FF, I can put the src change in a setTimeout with the tiniest wait
and it will execute both onload events like Opera does.

I can also get Opera to behave like firefox by having the iframe load
a page that doesn't load so fast.

So, it's just a timing issue it seems.

> 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?

Yeh. I think it should fire. I think it should fire when the iframe is
appended to the document and the resource loads also. Opera and FF do
this. It's just there are timing differences that produce different
behavior in some situations.

-- 
Michael


On 9/5/07, Jon Barnett <jonbarnett@gmail.com> wrote:
> 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
>


-- 
Michael

Received on Wednesday, 5 September 2007 15:29:16 UTC