Souders在他的blog里面有阐述:Using Iframes Sparingly:
- iframe会阻塞主页面的onload事件
- 主页面和iframe共享同一个连接池
动态引入iframe可以把iframe的加载放到onload事件后,但不好的地方是页面加载时间会更长;动态脚本如下:
<script type="text/javascript">
//doesn't block the load event
function createIframe() {
var i = document.createElement("iframe");
i.src = "/path/file";
i.setAttribute('frameborder', '0', 0);
i.width = "275px";
i.height = "260px";
document.getElementById("iframe_hold").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>