繁体   English   中英

如何将在父页面中声明的函数注入到iframe中,然后调用它?

[英]How do I inject a function declared in the parent page into an iframe, then call it?

我可以像这样注入iframe:

iframe.contentWindow.action...

但是,如果我想发送要在iframe中声明的函数,然后调用该函数,该怎么办?

例如

function hello(){...}
iframe.contentWindow...

并基本上都在iframe中声明和hello

只是为了澄清一下, hello的动作应该在iframe中完成。 例如,如果我放置document.getElementById("input").value="foo"; hello ,然后在iframe中调用它,则更改应在iframe内部的input生效,而不是在父窗口中生效。

将脚本标签插入其中包含代码的框架的<head>部分。 脚本标记可以是外部脚本参考,也可以是嵌入式脚本。 您必须是同一原产地才能进行注射。

var idoc = iframe.contentWindow.document;
var script = idoc.createElement("script");
script.textContent = 'document.getElementById("input").value="foo";'
idoc.getElementsByTagName("head")[0].appendChild(script);

暂无
暂无

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

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