简体   繁体   English

当父来自不同的域时,在另一个iframe中执行javascript函数

[英]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/page1B.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/page2B.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.

相关问题 当报表托管在Iframe中且其域与父页面的域不同时,无法从SSRS报表调用JavaScript - Calling JavaScript from SSRS Report is not working when the report is hosted in Iframe with domain different than that of Parent Page 从不同域中的父窗口获取iframe中的javascript变量 - Get javascript variable in an iframe from parent window in a different domain 在iframe上从父级执行JavaScript - Execute javascript from parent on iframe 在iframe中跨网域访问时,如何从父页面访问iframe javascript变量? - How to access iframe javascript variable from parent page when accessing cross domain in iframe? 在Iframe中从父窗口执行函数 - Execute function in Iframe from parent window 从具有不同于父域的域的iFrame滚动到iframe的顶部窗口 - Scroll window to top of Iframe from iFrame with different domain than parent 在来自其他域的iframe中运行Javascript - Running Javascript in an iframe from a different domain 允许子iframe从其他域调用其父窗口上的函数 - Allowing a child Iframe to call a function on its parent window from a different domain 允许父窗口在其他域的子iframe上调用函数 - Allowing a parent window to call a function on its child iframe of a different domain 来自外部域iframe的JavaScript调用功能 - Javascript call function from an external domain iframe
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM