开发者

how to access content from iframe in main window

开发者 https://www.devze.com 2023-04-12 20:54 出处:网络
How to access content from iframe in main window? I am using an iframe to post form to different domain, and I want to access return data from the iframe which I can append to my main window.

How to access content from iframe in main window?

I am using an iframe to post form to different domain, and I want to access return data from the iframe which I can append to my main window.

It开发者_JS百科 gives me error "access denied" when I try to access iframe elements.


You can't access one from the other if the iframe and the main page have a different domain.

You can exchange string data between the two with window.postMessage for modern browsers(IE8+) or using the window.name hack if you have to support older browsers.

Another option is to post to the other domain as you do, but giving an id. Then poll with a setInterval from the main page useing this id with a JSONP to get the response.


<script>$("#frameDemo").contents().find("a").css("background-color","#BADA55");</script>

http://api.jquery.com/contents/


If they are on different subdomains, but you have control over both domains, the solution is to add the following into a script tag:

document.domain = 'example.com';

Then you can chat between those iframes.

0

精彩评论

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

关注公众号