Reading Iframe Content in Iframe Load

2019-01-20 10:13发布

I just wanna learn how to read Iframe content in onLoad event of iframe and write it's content to the main page ? Thanks..

5条回答
【Aperson】
2楼-- · 2019-01-20 10:31
jQuery("#xiframe").load(function()
{
  var doc = null;
  try{
    doc = this.document || this.contentDocument || this.contentWindow && xiframe.contentWindow.document || null;
  } catch(err) { 
    alert('error: ' + err.description); 
  }

  try {             
   if(!doc) {
     alert('error');
     return false;
   }
  } catch(err) {
    alert('error: ' + err.description); 
    return false;
  }

  alert(String(jQuery(doc.body).html());
}
查看更多
欢心
3楼-- · 2019-01-20 10:33
  • parent document (the page on which the iframe is displayed)
  • child document (the page displayed inside the iframe)

If these two documents are in the same domain, you can use the window.parent.document object in the child document's onload event handler to access the parent document.

查看更多
神经病院院长
4楼-- · 2019-01-20 10:37
document.getElementById('iframeID').contentWindow.onload = function(){
    top.document.innerHTML = this.document.innerHTML;
}
查看更多
看我几分像从前
5楼-- · 2019-01-20 10:43

I have struggled with same this past day. It seems to matter how you access the iframe. If you use document.getElementById() you get an Iframe object, which has no onload event. However, if you access through window.frames[] array, for example

var iframeWindow = top.frames['iframeID'],

you get a window object, which does have onload event.

(ie uses the frame id attribute , but ff uses name attribute. So use both and make the same)

You can then assign

iframeWindow.onload=function(){iframeContent=iframeWindow.document.body.innerHTML;};

Notice, since iframeWindow is a window object, you use window syntax to access content.

查看更多
神经病院院长
6楼-- · 2019-01-20 10:48

You can use this jquery function in parent page and it also supports many other function to add and remove stuffs:

<script type="text/javascript">
        function myFunction() {
         var $currIFrame = $('#Frame1');
         $currIFrame.contents().find("body #idofElement").val('Value i want to set');
          }
</script>

In above code: #idofElement is the element from HTML page in iframe whose value you want to set. I Hope it helps You..

查看更多
登录 后发表回答