简体   繁体   English

在不使用parent.someFunction()的iframe中调用父函数?

[英]Calling a parent function from within an iframe WITHOUT using parent.someFunction()?

Is this possible? 这可能吗?

The function someMethod is a member of the parent window. 函数someMethod是父窗口的成员。

iframe content: iframe内容:

<a href="" onclick="Something.call('someMethod', 'Some data'); ">Send some data</a>

Any way to call someMethod without doing... 无需执行任何方法即可调用someMethod ...

<a href="" onclick="parent.Something.call('someMethod', 'Some data'); ">Send some data</a>

UPDATE: 更新:

Basically I have a presentation layer inside an iframe which has content that calls a metric api function to send a metric to the server. 基本上,我在iframe内有一个表示层,其内容可调用度量api函数以将度量发送到服务器。 the iframe is in the parent window. iframe位于父窗口中。 The content (presentation) was previously developed without using a parent.SomeAPIMethod() call. 内容(演示文稿)以前是在不使用parent.SomeAPIMethod()调用的情况下开发的。 How do I intercept the api call from the parent layer? 如何拦截来自父层的api调用? Any way to do this without changing all the metric calls within the content? 有什么办法可以在不更改内容中所有指标调用的情况下进行操作?

You can use into a frame or iframe: 您可以在框架或iframe中使用:

  1. window.parent.someFunction(parameters) to call a function in a window up one level. window.parent.someFunction(parameters)在上一级窗口中调用函数。
  2. window.top.someFunction(parameters) to call a function the maximum level window. window.top.someFunction(parameters)将函数调用为最大级别窗口。 In others words, the container window of all. 换句话说,所有的容器窗口。

For example, this is the JavaScript in the window container: 例如,这是窗口容器中的JavaScript:

function someFunction(msg) {
    alert(msg +"!!!");
}

In the frame or iframe you can use with a button: 在框架或iframe中,您可以使用按钮:

<button onclick="window.top.someFunction('message');">Trigger function</button>

However, if you want to use a "alias", then you can use this JavaScript: 但是,如果要使用“别名”,则可以使用以下JavaScript:

var aliasFn = window.top.someFunction;

After, when you want to use it: 之后,当您要使用它时:

<button onclick="aliasFn('message');">Trigger function</button>

Hope that helps. 希望能有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM