简体   繁体   English

从iframe调用在父级中定义的javascript方法

[英]Call javascript method defined in parent from iframe

After some research (even at stackoverflow) I still can't figure out how to do this. 经过一些研究(甚至在stackoverflow上),我仍然不知道如何执行此操作。 parent.method() won't do the trick, nor some other solutions I've tried. parent.method()不会解决问题,我也不会尝试其他解决方案。

Situation: I have a index.html on the client side (mobile phone in this case) which has an iframe loading server-side page. 情况:我在客户端(在本例中为手机)上有一个index.html ,其中有一个iframe加载服务器端页面。 What I need to do is call a javascript method defined in the index.html (client side) from the iframe content (server-side). 我需要做的是从iframe内容(服务器端)调用index.html (客户端)中定义的javascript方法。

As an example (I'm not using android in the question described above), Android apps have addJavascriptInterface which, when defined, allows one to call methods defined client-side from server-side pages just invoking window.CustomObject.MethodToCall() . 例如(我在上述问题中未使用android),Android应用程序具有addJavascriptInterface ,在定义时,它允许仅调用window.CustomObject.MethodToCall()从服务器端页面调用客户端定义的方法。

Any hint? 有什么提示吗?

Thanks! 谢谢!

In certain situation there could be a neccessity of calling a javascript function inside an iframe from the parent document, and vice versa ie; 在某些情况下,可能有必要从父文档的iframe中调用javascript函数,反之亦然。 calling a javascript function in parent document from the iframe. 从iframe调用父文档中的javascript函数。

For example; 例如; the parent document have an iframe with id attribute 'iFrameId', and the function 'functionInIframe()' is defined in that iframe document. 父文档具有ID属性为“ iFrameId”的iframe,并且在该iframe文档中定义了函数“ functionInIframe()”。 Following code can call that iframe function from the parent document itself. 以下代码可以从父文档本身调用该iframe函数。

document.getElementById('iFrameId').contentWindow.functionInIframe();

And following code can call the function defined in parent document(functionInParent()) from the iframe itself. 以下代码可以从iframe本身调用父文档(functionInParent())中定义的函数。

parent.functionInParent();

This way javascript can interact between parent document and iframe. 这种javascript可以在父文档和iframe之间进行交互。

This is the original post . 这是原始帖子

window.top.foo 

for the top level window 用于顶层窗口

window.parent.foo

for the direct parent 对于直接父母

I realize I am only a year late to this party but there was no real answer. 我知道我参加这个聚会只晚了一年,但没有真正的答案。

So, in order to do this both files must be on the same domain. 因此,为了做到这一点,两个文件必须位于同一域中。 Since you have the index.html on the phones localhost and load a page on your site it will not work (locahost to example.com). 由于您在电话本地主机上具有index.html并在您的站点上加载了一个页面,因此该页面将无法工作(定位到example.com的主机)。 You could load the index.html off your site as well and that would fix this problem (example.com to example.com). 您也可以将index.html加载到您的网站之外,这样可以解决此问题(将example.com更改为example.com)。 Then you could reference the parent frame in the normal window.top.function . 然后,您可以在常规window.top.function引用父框架。

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

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