开发者

How do I get the innerHTML of an iframe?

开发者 https://www.devze.com 2023-02-23 05:24 出处:网络
I have a big big trouble. :( I\'m trying to get an iFrame innerHTML in order to put that in a div innerHTML. I\'ve tried everything I found on go开发者_如何学Google. Is there any chance to help me wit

I have a big big trouble. :( I'm trying to get an iFrame innerHTML in order to put that in a div innerHTML. I've tried everything I found on go开发者_如何学Google. Is there any chance to help me with that? Thanks!


My guess is the site in the iframe does not have the same domain, protocol and port as its parent, and therefore, can not be accessed.

This is by design. Check out the Same Origin Policy.

If you are on http://sub.domain.com and want to access http://domain.com, you could use...

document.domain = 'domain.com';

Documentation @ MDC.

As for getting the innerHTML, try...

var iframe = document.getElementById('iframe'),
    iframeDocument;

if ('contentWindow' in iframe) {
   iframeDocument = iframe.contentWindow;
} else {
   iframeDocument = iframe.contentDocument;
}

var innerHTML = iframeDocument.innerHTML;
0

精彩评论

暂无评论...
验证码 换一张
取 消