-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathIframeExample1.html
More file actions
64 lines (50 loc) · 1.69 KB
/
IframeExample1.html
File metadata and controls
64 lines (50 loc) · 1.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Iframe框架</title>
</head>
<body>
<iframe
id="iframeElement"
src="./IframePage.html"
></iframe>
<!--<iframe-->
<!-- id="iframeElement"-->
<!-- src="http://localhost:8088/webUtils/static/IframePage.html"-->
<!--></iframe>-->
<script>
const INJECT_JS = `
window.APPBridge = {
post: function(){
alert('APPBridge.post');
}
};
`;
const iframeElement = document.getElementById("iframeElement");
//创建一个script标签
function loadScriptString(iframeDocument, code) {
let script = iframeDocument.createElement("script"); //创建一个script标签
script.type = "text/javascript";
try {
//IE浏览器认为script是特殊元素,不能再访问子节点;报错;
script.appendChild(iframeDocument.createTextNode(code));
} catch (ex) {
script.text = code;
}
iframeDocument.getElementsByTagName('head')[0].appendChild(script);
}
window.onload = function () {
const contentWindow = iframeElement.contentWindow;
const iframeDocument = iframeElement.contentWindow.document;
// 调用iframe页面的JS 方法
// contentWindow.testMethod && contentWindow.testMethod();
loadScriptString(iframeDocument, INJECT_JS);
// iframeElement.srcdoc = INJECT_JS;
console.log("contentWindow", iframeElement.contentWindow);
console.log("contentWindow.document", iframeElement.contentWindow.document);
console.log("iframeElement.srcdoc", iframeElement.srcdoc);
}
</script>
</body>
</html>