基本上,我在页面中嵌入了一个iframe,该iframe有一些需要从父页面调用的JavaScript例程。

现在相反的是相当简单,因为你只需要调用parent.functionName(),但不幸的是,我需要的恰恰相反。

请注意,我的问题不是改变iframe的源URL,而是调用iframe中定义的函数。


当前回答

从iframe调用父JS函数是可能的,但只有当父JS函数和加载在iframe中的页面都来自同一个域,即example.com,并且都使用相同的协议,即都在http://或https://.上

在以下情况下调用将失败:

父页面和iframe页面来自不同的域。 他们使用不同的协议,一个在http://上,另一个在https://.上

任何解决这个限制的方法都是非常不安全的。

例如,假设我注册了域名superwinningcontest。例如,发送链接到人们的电子邮件。当他们加载主页时,我可以在那里隐藏一些iframe,阅读他们的Facebook动态,查看最近的亚马逊或贝宝交易,或者(如果他们使用的服务没有实现足够的安全性)从他们的账户转钱。这就是JavaScript仅限于同一域和同一协议的原因。

其他回答

我找到了一个很好的解决办法。

正如您所说,执行位于父文档中的代码相当容易。这是我代码的基础,做的正好相反。

当我的iframe加载时,我调用父文档上的一个函数,将一个引用作为参数传递给位于iframe文档中的一个局部函数。 父文档现在可以通过这个引用直接访问iframe的函数。

例子:

在父节点上:

function tunnel(fn) {
    fn();
}

在iframe中:

var myFunction = function() {
    alert("This work!");
}

parent.tunnel(myFunction);

当iframe加载时,它将调用parent.tunnel(YourFunctionReference),它将执行parameter中接收到的函数。

这很简单,不需要处理来自各种浏览器的所有非标准方法。

这里有一些需要注意的怪癖。

HTMLIFrameElement.contentWindow is probably the easier way, but it's not quite a standard property and some browsers don't support it, mostly older ones. This is because the DOM Level 1 HTML standard has nothing to say about the window object. You can also try HTMLIFrameElement.contentDocument.defaultView, which a couple of older browsers allow but IE doesn't. Even so, the standard doesn't explicitly say that you get the window object back, for the same reason as (1), but you can pick up a few extra browser versions here if you care. window.frames['name'] returning the window is the oldest and hence most reliable interface. But you then have to use a name="..." attribute to be able to get a frame by name, which is slightly ugly/deprecated/transitional. (id="..." would be better but IE doesn't like that.) window.frames[number] is also very reliable, but knowing the right index is the trick. You can get away with this eg. if you know you only have the one iframe on the page. It is entirely possible the child iframe hasn't loaded yet, or something else went wrong to make it inaccessible. You may find it easier to reverse the flow of communications: that is, have the child iframe notify its window.parent script when it has finished loaded and is ready to be called back. By passing one of its own objects (eg. a callback function) to the parent script, that parent can then communicate directly with the script in the iframe without having to worry about what HTMLIFrameElement it is associated with.

在IFRAME中,让你的函数对窗口对象是公共的:

window.myFunction = function(args) {
   doStuff();
}

从父页面访问,使用这个:

var iframe = document.getElementById("iframeId");
iframe.contentWindow.myFunction(args);

使用following调用父页中帧的函数

parent.document.getElementById('frameid').contentWindow.somefunction()

下面是Nitin Bansal的回答

为了更健壮:

function getIframeWindow(iframe_object) {
  var doc;

  if (iframe_object.contentWindow) {
    return iframe_object.contentWindow;
  }

  if (iframe_object.window) {
    return iframe_object.window;
  } 

  if (!doc && iframe_object.contentDocument) {
    doc = iframe_object.contentDocument;
  } 

  if (!doc && iframe_object.document) {
    doc = iframe_object.document;
  }

  if (doc && doc.defaultView) {
   return doc.defaultView;
  }

  if (doc && doc.parentWindow) {
    return doc.parentWindow;
  }

  return undefined;
}

and

...
var el = document.getElementById('targetFrame');

var frame_win = getIframeWindow(el);

if (frame_win) {
  frame_win.targetFunction();
  ...
}
...