Reliably detect when an iframe has loaded?

Discussion in 'Javascript' started by Peter Michaux, Jun 16, 2009.

  1. I have a form which posts to a hidden iframe. The iframe is the form's
    target. This is because the form must initiate a file download. So the
    form is in the parent window and the iframe is the child.

    There is potential that the server sends an error response to the
    child window. I'm being asked to detect that error response from the
    parent window so that the server-side programmers do not have to
    change what they send as the response.

    My suggestion was to change the server's response and have the server
    send back a page with a snip of JavaScript to cause action in the
    parent window. Something like "window.parent.location="/login/page"
    for a session timeout. As far as I know, having the server send back
    some JavaScript is the only standard way to have the parent document
    take action based on an error loading in the iframe. This has been
    judged too onerous a change to the server-side programming by the
    server-side programmers.

    So in case I don't know about something I could be using in this
    situation, is there a standard, reliable way to detect that the iframe
    has loaded its contents? If so I could then parse the iframe's
    contents to detect an error.

    Thanks,
    Peter
     
    Peter Michaux, Jun 16, 2009
    #1
    1. Advertising

  2. Peter Michaux, Jun 16, 2009
    #2
    1. Advertising

  3. Peter Michaux

    Jorge Guest

    On Jun 16, 7:28 pm, Peter Michaux <> wrote:
    > On Jun 16, 10:08 am, Peter Michaux <> wrote:
    >
    > > So in case I don't know about something I could be using in this
    > > situation, is there a standard, reliable way to detect that the iframe
    > > has loaded its contents?

    >
    > The "load" event on an iframe doesn't seem to be standard.
    >
    > http://www.w3.org/TR/DOM-Level-2-Events/events.html#Events-eventgroup...
    >
    > Peter


    This seems to work... :

    (function isLoaded() {
    if (iframe.contentWindow && iframe.contentWindow.document &&
    iframe.contentWindow.document.body &&
    iframe.contentWindow.document.body.innerHTML) {
    alert("ONLOAD: "+ iframe.contentWindow.document.body.innerHTML);
    } else {
    setTimeout(isLoaded, 333);
    }
    })();

    --
    Jorge.
     
    Jorge, Jun 16, 2009
    #3
  4. Peter Michaux

    Jorge Guest

    On Jun 16, 8:13 pm, Jorge <> wrote:
    >
    > This seems to work... :
    > (...)


    .... for iframes coming from the same domain...

    http://jorgechamorro.com/cljs/063/

    --
    Jorge.
     
    Jorge, Jun 16, 2009
    #4
  5. On Jun 16, 11:13 am, Jorge <> wrote:
    > On Jun 16, 7:28 pm, Peter Michaux <> wrote:
    >
    > > On Jun 16, 10:08 am, Peter Michaux <> wrote:

    >
    > > > So in case I don't know about something I could be using in this
    > > > situation, is there a standard, reliable way to detect that the iframe
    > > > has loaded its contents?

    >
    > > The "load" event on an iframe doesn't seem to be standard.

    >
    > >http://www.w3.org/TR/DOM-Level-2-Events/events.html#Events-eventgroup...

    >
    > > Peter

    >
    > This seems to work... :
    >
    > (function isLoaded() {
    >   if (iframe.contentWindow && iframe.contentWindow.document &&
    > iframe.contentWindow.document.body &&
    > iframe.contentWindow.document.body.innerHTML) {
    >     alert("ONLOAD: "+ iframe.contentWindow.document.body.innerHTML);
    >   } else {
    >     setTimeout(isLoaded, 333);
    >   }
    >
    > })();


    Thanks for the suggestion. I actually did think about polling but I
    was thinking about polling for another property only introduced in
    IE8. It seems contentWindow is all the way back to IE5.5. Polling does
    make me worry, however, although that may be unjustified worrying.

    I think the problem with polling for my situation is that if the
    request is successful, that is a file is downloaded, the iframes
    document body is not affected in a predictable way. So I don't think
    there would be a predictable way to stop the polling. Perhaps that
    wouldn't matter.

    Even though the "load" event of an iframe is non-standard, it does
    seem to be a de facto standard. I see that other libraries like YUI
    depend upon the load event and so it is unlikely future browsers will
    be able to not have that event. I think I'm going to go with the
    iframe "load" event for now.

    Peter
     
    Peter Michaux, Jun 16, 2009
    #5
  6. Peter Michaux

    Jorge Guest

    On Jun 16, 9:17 pm, Peter Michaux <> wrote:
    > (...)
    > Even though the "load" event of an iframe is non-standard, it does
    > seem to be a de facto standard. (...)


    Hmm, first I wrote:

    iframe.onload= function () {
    alert("onload event handler");
    };

    and it didn't work in IE6 nor 7. Instead, iframe.attachEvent does :

    http://jorgechamorro.com/cljs/063/
    --
    Jorge.
     
    Jorge, Jun 16, 2009
    #6
  7. On Jun 16, 12:36 pm, Jorge <> wrote:
    > On Jun 16, 9:17 pm, Peter Michaux <> wrote:
    >
    > > (...)
    > > Even though the "load" event of an iframe is non-standard, it does
    > > seem to be a de facto standard. (...)

    > Hmm, first I wrote:
    >
    > iframe.onload= function () {
    >   alert("onload event handler");
    >
    > };
    >
    > and it didn't work in IE6 nor 7. Instead, iframe.attachEvent does :


    Yes, I was using attachEvent in IE also and it worked for me. I didn't
    try the onload property.

    It seems to me the lack of the load event for iframes may have been a
    specification bug. I don't know why they would include the load event
    for frames in a frameset but exclude on purpose for an iframe.

    Peter
     
    Peter Michaux, Jun 16, 2009
    #7
  8. Jorge wrote:
    > On Jun 16, 9:17 pm, Peter Michaux <> wrote:
    >> (...)
    >> Even though the "load" event of an iframe is non-standard, it does
    >> seem to be a de facto standard. (...)

    >
    > Hmm, first I wrote:
    >
    > iframe.onload= function () {
    > alert("onload event handler");
    > };
    >
    > and it didn't work in IE6 nor 7. Instead, iframe.attachEvent does :
    >


    For IE I've been using, and never thought to try attachEvent:

    var doc = document.getElementById("myFrame");
    doc.onreadystatechange = function() {
    if (doc.readyState == "complete") {
    // blah
    }
    };

    Andrew Poulos
     
    Andrew Poulos, Jun 16, 2009
    #8
  9. On Jun 16, 6:00 pm, Peter Michaux <> wrote:
    > On Jun 16, 12:36 pm, Jorge <> wrote:
    >
    > > On Jun 16, 9:17 pm, Peter Michaux <> wrote:

    >
    > > > (...)
    > > > Even though the "load" event of an iframe is non-standard, it does
    > > > seem to be a de facto standard. (...)

    > > Hmm, first I wrote:

    >
    > > iframe.onload= function () {
    > >   alert("onload event handler");

    >
    > > };

    >
    > > and it didn't work in IE6 nor 7. Instead, iframe.attachEvent does :

    >
    > Yes, I was using attachEvent in IE also and it worked for me. I didn't
    > try the onload property.
    >
    > It seems to me the lack of the load event for iframes may have been a
    > specification bug. I don't know why they would include the load event
    > for frames in a frameset but exclude on purpose for an iframe.


    Regardless of being present in most user agents, since it's not on the
    standards, I'd mix both approaches to make sure that it works
    everywhere.

    --
    Gabriel Gilini
     
    Gabriel Gilini, Jun 17, 2009
    #9
  10. On 6/16/2009 10:08 AM, Peter Michaux wrote:
    > I have a form which posts to a hidden iframe. The iframe is the form's
    > target. This is because the form must initiate a file download. So the
    > form is in the parent window and the iframe is the child.
    >
    > There is potential that the server sends an error response to the
    > child window. I'm being asked to detect that error response from the
    > parent window so that the server-side programmers do not have to
    > change what they send as the response.
    >
    > My suggestion was to change the server's response and have the server
    > send back a page with a snip of JavaScript to cause action in the
    > parent window. Something like "window.parent.location="/login/page"
    > for a session timeout. As far as I know, having the server send back
    > some JavaScript is the only standard way to have the parent document
    > take action based on an error loading in the iframe. This has been
    > judged too onerous a change to the server-side programming by the
    > server-side programmers.
    >
    > So in case I don't know about something I could be using in this
    > situation, is there a standard, reliable way to detect that the iframe
    > has loaded its contents? If so I could then parse the iframe's
    > contents to detect an error.


    untested...


    var t = document.getElemmentById("iframeId");

    ///before submit...
    t.documentElement.clear();
    t.documentElement.write("OK");
    t.documentElement.close();

    //submit form...
    ....

    //check in 5 seconds...
    window.setTimeout(function(){
    if (t.documentElement.innerHTML != "OK") {
    ...handle error...
    }
    }, 5000);

    --
    Michael J. Ryan - http://tracker1.info/

    .... B5: I can only conclude that I'm paying off karma at a vastly accelerated
    rate.
     
    Michael J. Ryan, Jun 18, 2009
    #10
  11. doh! (iframe.contentWindow && iframe.contentWindow.document)
    not iframe.documentElement

    --
    Michael J. Ryan - http://tracker1.info/

    .... "Immortality consists largely of boredom." -- Zephrem Cochrane
     
    Michael J. Ryan, Jun 18, 2009
    #11
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?QW5kcmV3?=

    How to reliably redirect from login page?

    =?Utf-8?B?QW5kcmV3?=, Jan 10, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    388
    =?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=
    Jan 10, 2006
  2. Maflu

    How to Detect Flash Reliably

    Maflu, Oct 29, 2003, in forum: Javascript
    Replies:
    0
    Views:
    102
    Maflu
    Oct 29, 2003
  3. Daedalous
    Replies:
    3
    Views:
    718
    Daedalous
    Jan 16, 2004
  4. Mark
    Replies:
    4
    Views:
    113
    Java script Dude
    Aug 5, 2004
  5. Replies:
    5
    Views:
    109
Loading...

Share This Page