当父项来自不同的域时,在另一个iframe中执行javascript函数 [英] execute javascript function in a another iframe when parent is from different domain
问题描述
网页 A.com 有2个iframes B.com/page1 和 B.com/page2 。
这是A.com的代码:
< html>< body&
< iframe src =b.com/page1name =iframe1id =iframe1>
< iframe src =b.com/page2>
< / body>< / html>
我要在 B.com/page1 上执行js函数 B.com/page2 。
下面的两个例子都适用于父类来自同一个域但不是跨域的情况:
parent。 window.frames ['iframe1']。SomeFunction(args);
或
parent.document.getElementById('iframe1')。contentWindow.SomeFunction(args);有没有办法吗?
< h2_lin>解决方案而不是
parent.window.frames ['iframe1']。SomeFunction (args);
使用
parent.frames ['iframe1']。SomeFunction(args);
您可以遍历 frames
你可以引用任何窗口,但在这种情况下,你试图遍历递归窗口属性 parent
这是窗口)。这是不允许的。
The page A.com has 2 iframes B.com/page1 and B.com/page2.
This is the code of 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.
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). This is not allowed.
这篇关于当父项来自不同的域时,在另一个iframe中执行javascript函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!