当父项来自不同的域时,在另一个iframe中执行javascript函数 [英] execute javascript function in a another iframe when parent is from different domain

查看:284
本文介绍了当父项来自不同的域时,在另一个iframe中执行javascript函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

网页 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆