I have 2 html files.
我有2个html文件。
1.html will call 2.html from iframe.
1.html将从iframe调用2.html。
<iframe src="2.html" > </iframe>
2.html defined 2 javascript functions.
2.html定义了2个javascript函数。
function func1(a, b, c) {
for () {
.....
}
}
function func2() {
func1(a, b, c);
}
HTML
<body onload="func2()">
Now my question is, I have to merge these 2 html files to one html. 2.html must be executed inside the iframe.
现在我的问题是,我必须将这两个html文件合并为一个html。 2.html必须在iframe内执行。
I tried to do by,
我试过做,
<!DOCTYPE html>
<html>
<body>
<iframe id="foo" onload="myFunction()" /> </iframe>
<script>
function myFunction() {
var iframe = document.getElementById('foo'),
iframedoc = iframe.contentDocument || iframe.contentWindow.document;
iframedoc.body.innerHTML = func2();
}
</script>
</body>
</html>
But func2() is not called. Could anyone suggest the way to call javascript functions inside iframe?
但是没有调用func2()。任何人都可以建议在iframe中调用javascript函数的方法吗?
Thanks in advance.
提前致谢。
1 个解决方案
#1
2
You can't call functions from iframe like that, due to CSP(Content Security Policy). The only way to comunicate outside of the iframe is with message passing. See this for explanation https://developer.mozilla.org/en-US/docs/Web/API/Window/postMessage .
由于CSP(内容安全策略),您无法像这样从iframe调用函数。在iframe之外进行通信的唯一方法是使用消息传递。请参阅此说明https://developer.mozilla.org/en-US/docs/Web/API/Window/postMessage。
#1
2
You can't call functions from iframe like that, due to CSP(Content Security Policy). The only way to comunicate outside of the iframe is with message passing. See this for explanation https://developer.mozilla.org/en-US/docs/Web/API/Window/postMessage .
由于CSP(内容安全策略),您无法像这样从iframe调用函数。在iframe之外进行通信的唯一方法是使用消息传递。请参阅此说明https://developer.mozilla.org/en-US/docs/Web/API/Window/postMessage。