简体   繁体   English

如何在主窗口中从iframe访问内容

[英]how to access content from iframe in main window

How to access content from iframe in main window? 如何在主窗口中从iframe访问内容?

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. 我正在使用iframe将表单发布到其他域,并且我想访问iframe的返回数据,并将其附加到主窗口中。

It gives me error "access denied" when I try to access iframe elements. 当我尝试访问iframe元素时,出现错误“访问被拒绝”。

You can't access one from the other if the iframe and the main page have a different domain. 如果iframe和主页具有不同的域,则无法访问彼此。

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. 您可以使用现代浏览器(IE8 +)的window.postMessage在两者之间交换字符串数据,或者,如果必须支持较旧的浏览器,则可以使用window.name hack。

Another option is to post to the other domain as you do, but giving an id . 另一种选择是像您一样发布到另一个域,但要提供一个id Then poll with a setInterval from the main page useing this id with a JSONP to get the response. 然后从主页使用setInterval轮询,并使用此idJSONP来获取响应。

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. 然后,您可以在这些iframe之间聊天。

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM