最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Dynamically create an iframe and attach onload event to it - Stack Overflow

programmeradmin6浏览0评论

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 event to iframe to read the iframe content. Can somebody suggest how do I do that?

frame = document.createElement('iframe');
frame.setAttribute('src','');
body.appendChild(frame);
frame.onload = function(){
    alert('hi'); // here I want to read the content in the frame.
}

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 event to iframe to read the iframe content. Can somebody suggest how do I do that?

frame = document.createElement('iframe');
frame.setAttribute('src','http://example.com');
body.appendChild(frame);
frame.onload = function(){
    alert('hi'); // here I want to read the content in the frame.
}
Share Improve this question edited May 13, 2020 at 6:30 Brian Tompsett - 汤莱恩 5,88372 gold badges61 silver badges133 bronze badges asked May 31, 2011 at 6:24 sushil bharwanisushil bharwani 30.2k30 gold badges97 silver badges130 bronze badges
Add a comment  | 

4 Answers 4

Reset to default 15

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);

You can add a load event listener to the iframe's contentWindow, which is just like any other Window.

Here's an example:

      iframe = document.createElement('iframe')
      iframe.setAttribute('src', 'https://example.com/')

      document.getElementsByTagName('body')[0].appendChild(iframe)
      const onLoaded = function() {
        console.log('iframe loaded');
      }
      if (iframe.contentWindow.document.readyState === 'complete') {
        console.log('already loaded')
        onLoaded()
      } else {
        iframe.contentWindow.addEventListener('load', onLoaded)
      }

I just tried this and it worked in Chrome:

var iframe = document.createElement('iframe');
// append iframe to DOM however you like then:
iframe.contentWindow.parent.location.href // properly gives parent window's href.

W3school says contentWindow is supported in all major browsers.

I doubt you can attach an onload event to an iframe. It will not work on all browsers. You can on the other hand check if the iframe was loaded by:

window.onload=function()
{
var iframe=document.getElementById('myframe');
if(iframe) 
    alert('The iframe has just been loaded.');
}

Or use AJAX to load the content in your container. With AJAX you can set a proper load-complete event.

发布评论

评论列表(0)

  1. 暂无评论