簡體   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