-
Notifications
You must be signed in to change notification settings - Fork 0
/
异步iframe.html
65 lines (59 loc) · 2.53 KB
/
异步iframe.html
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
65
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>iframe</title>
</head>
<body>
<!-- <iframe id="iframe0" src="http://www.sina.com" frameborder="0" width="728" height="90" scrolling="auto"> </iframe> -->
<div id="iframe-wrapper"></div>
<img src="http://n.sinaimg.cn/tech/640/w396h244/20180420/yeMq-fznefkf4584324.gif" alt="">
<script>
// console.log(document.getElementById('iframe0').contentWindow.document)
//doesn't block the load event
function createIframe() {
var i = document.createElement("iframe")
i.src = "https://segmentfault.com/"
i.scrolling = "auto"
i.frameborder = "0"
i.width = "200px"
i.height = "100px"
document.getElementById("iframe-wrapper").appendChild(i)
}
// Check for browser support of event handling capability
if (window.addEventListener) {
window.addEventListener("load", createIframe, false)
} else if (window.attachEvent) {
window.attachEvent("onload", createIframe)
} else {
window.onload = createIframe
}
</script>
<div>
<iframe id="iframe1" src="" width="200" height="100" border="2"></iframe>
</div>
<script>
function setIframeSrc() {
var url = "https://segmentfault.com/";
var iframe1 = document.getElementById('iframe1');
if (-1 == navigator.userAgent.indexOf("MSIE")) {
iframe1.src = url;
} else {
iframe1.location = url;
}
}
setTimeout(setIframeSrc, 0);
</script>
<script>
(function (document) {
var iframe = document.body.appendChild(document.createElement('iframe')),
doc = iframe.contentWindow.document;
// style the iframe with some CSS
// iframe.style.cssText = "position:absolute;width:200px;height:100px;left:300px;";
// doc.open().write('<body onload="' + 'var docEl = document;docEl.getElementsByTagName(\'head\')[0].' + 'appendChild(docEl.createElement(\'script\')).src' + '=\'\/path\/to\/file\'">');
doc.open().write('<body onload="' + 'var docEl = document;docEl.getElementsByTagName(\'head\')[0].' + 'appendChild(docEl.createElement(\'script\')).src' + '=\'https:\/\/segmentfault.com\/\'">');
doc.close(); //iframe onload event happens
})(document);
</script>
</body>
</html>