[英]execute javascript function in a another iframe when parent is from different domain
The page A.com has 2 iframes B.com/page1 and B.com/page2 . 页面A.com有2个iframes B.com/page1和B.com/page2 。 This is the code of A.com: 这是A.com的代码:
<html><body>
<iframe src="b.com/page1" name="iframe1" id="iframe1">
<iframe src="b.com/page2">
</body></html>
I want to execute js function on B.com/page1 from B.com/page2 . 我想从B.com/page2在B.com/page1上执行js功能。 Both examples below works well when the parent is from the same domain but not in cross domain scenario: 当父来自同一个域而不是跨域方案时,以下两个示例都很有效:
parent.window.frames['iframe1'].SomeFunction(args);
or 要么
parent.document.getElementById('iframe1').contentWindow.SomeFunction(args);
Is there any way to do it? 有什么办法吗?
Instead of 代替
parent.window.frames['iframe1'].SomeFunction(args);
use 采用
parent.frames['iframe1'].SomeFunction(args);
You are allowed to traverse the frames
collection of any window that you can reference, but in this case you are trying to traverse the recursive window
property of parent
(which IS the window). 您可以遍历可以引用的任何窗口的frames
集合,但在这种情况下,您尝试遍历parent
的递归window
属性(即窗口)。 This is not allowed. 这是不允许的。
不,浏览器不允许根本不在同一域的iframe之间进行交互。
As Aaron have already told you, browsers don't allow this, but there are ways around it. 正如Aaron已经告诉过你的那样,浏览器不允许这样做,但有很多方法可以解决它。 You'll need to create a small hack. 你需要创建一个小黑客。 Eugene Gladyshev has a post on it on his blog . Eugene Gladyshev 在他的博客上有一篇文章 。
As Aaron states, it is not allowed to interact between iframes, but you should take a look at easyXDM, it helps with cross domain communication in javascript: 正如Aaron所说,不允许在iframe之间进行交互,但你应该看一下easyXDM,它有助于javascript中的跨域通信:
http://easyxdm.net/wp/ http://easyxdm.net/wp/
Here is the example you might be looking for: 以下是您可能正在寻找的示例:
http://easyxdm.net/wp/2010/03/17/sending-and-receiving-messages/ http://easyxdm.net/wp/2010/03/17/sending-and-receiving-messages/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.