Dynamically create an iframe and attach onload event to it

后端 未结 4 2134
隐瞒了意图╮
隐瞒了意图╮ 2021-02-08 05:20

I have created a iframe dynamically and added a src attribute to it. Then I have appended this iframe to body of the page. Know I want to attach an onload

4条回答
  •  不知归路
    2021-02-08 05:52

    Some browsers do have the onload event for an iframe, first you should try to attach it before setting the iframe's src attribute.

    I'd avoid using it altogether since in certain browsers it might not fire under certain conditions (e.g. the target was in cache in IE).

    You could user a timer to check if the frame's contentWindow's readystate is complete

    var inter = window.setInterval(function() {
        if (frame.contentWindow.document.readyState === "complete") {
          window.clearInterval(inter);
          // grab the content of the iframe here
        }
    }, 100);
    

提交回复
热议问题