【发布时间】:2011-06-06 11:14:15
【问题描述】:
我想隐藏加载到 iFrame 中的网站元素。脚本和站点可能需要一段时间,因此要求 iFrame 内容仅在修改后才显示。
我使用 onload 事件来执行隐藏元素的脚本。我可以访问 iFrame 内容(它在同一个域中,但不是同一个服务器/网络)。
function frameLoaded() {
sleep(3000); //simulating the time the script needs
var leftDiv = window.frames.testFrame.document.getElementById("leftcol");
leftDiv.style.display = "none";
}
简单的睡眠功能:
function sleep(milliSeconds){
var startTime = new Date().getTime(); // get the current time
while (new Date().getTime() < startTime + milliSeconds); // hog cpu
}
第一次加载带有 iFrame 的网站时,会显示滚动条和一些样式元素,但没有内容文本:
http://img651.imageshack.us/img651/5082/beforeu.png
脚本完成后,页面将按原样显示(天蓝色的 div 被隐藏):
http://img51.imageshack.us/img51/6806/afterq.png
当脚本运行时,不应显示任何内容。一个空的 iframe 或整个网站等待加载完成就足够了。
【问题讨论】:
-
您是否正在加载您自己的网站之一?还是第三方网站?我只问,因为如果您可以向 IFrame 中加载的网站添加一些 javascript,您可以输入以下行 document.body.onload = function() {parent.frameLoaded();} 并摆脱所有这些睡眠函数调用。
-
第三方。睡眠只是为了模拟“长”脚本执行时间。显然,脚本确实可能需要一些时间。这将是相当复杂的,也可以通过智能手机访问
标签: javascript iframe internet-explorer-8 coding-style onload