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