iframe的Javascript动态创建;t加载src

Javascript dynamic creation of iframe doesn't load src

本文关键字:加载 src 创建 Javascript 动态 iframe      更新时间:2023-09-26

我有下面的一段代码,应该以谷歌主页为例动态加载iframe。创建了iframe,但没有加载src:

var theIframe2 = document.createElement("iframe");
theIframe2.setAttribute("id", "main-iframe2");
theIframe2.setAttribute("src", "http://www.google.co.uk");
theIframe2.style.width = "300px";
theIframe2.style.height = "200px"; 
theIframe2.style.position = "absolute";
theIframe2.style.top = "0px";
theIframe2.frameBorder = 1;
var containerDiv2 = document.getElementById('container');
containerDiv2.appendChild(theIframe2);

它也在jsfiddle上:http://jsfiddle.net/JxhFE/1/

如有任何建议,我们将不胜感激。

谢谢,科林。

您不能在域中的IFrame中显示google.co.uk。由于安全原因,Google只允许从同一来源嵌入其主页。(X-Frame-Options SAMEORIGIN标头。)