Detecting when Iframe content has loaded (Cross browser)

前端 未结 4 1935
自闭症患者
自闭症患者 2020-11-29 00:51

I\'m trying to detect when an iframe and its content have loaded but not having much luck. My application takes some input in text fields in the parent window and updates th

4条回答
  •  感情败类
    2020-11-29 01:16

    to detect when the iframe has loaded and its document is ready?

    It's ideal if you can get the iframe to tell you itself from a script inside the frame. For example it could call a parent function directly to tell it it's ready. Care is always required with cross-frame code execution as things can happen in an order you don't expect. Another alternative is to set ‘var isready= true;’ in its own scope, and have the parent script sniff for ‘contentWindow.isready’ (and add the onload handler if not).

    If for some reason it's not practical to have the iframe document co-operate, you've got the traditional load-race problem, namely that even if the elements are right next to each other:

    
    
    

    there is no guarantee that the item won't already have loaded by the time the script executes.

    The ways out of load-races are:

    1. on IE, you can use the ‘readyState’ property to see if something's already loaded;

    2. if having the item available only with JavaScript enabled is acceptable, you can create it dynamically, setting the ‘onload’ event function before setting source and appending to the page. In this case it cannot be loaded before the callback is set;

    3. the old-school way of including it in the markup:

    Inline ‘onsomething’ handlers in HTML are almost always the wrong thing and to be avoided, but in this case sometimes it's the least bad option.

提交回复
热议问题