您可以使用iframe.load 事件来做到这一点。
您要做的是在页面加载时隐藏 iframe 并显示加载屏幕,然后您想等到内容加载完毕然后显示框架并隐藏加载屏幕。
(本示例假设您使用 IFrame 的 src 属性来加载内容)
纯Javascript:Example JSFiddle
var frame = document.getElementById('iframeID');
var loading = document.getElementById('loadingScreen');
frame.style.display = 'none';//Originally hide the frame
loading.style.display = 'block';//Originally show the Loading Screen
frame.src = 'http://www.bing.com';//Set the src attribute
frame.onload = function() {
frame.style.display = 'block';//Show the frame after it is loaded
loading.style.display = 'none';//Hide the loading screen
}
编辑:(删除 jQuery 示例并添加基于评论的新示例)
这是一个新示例,它检查子页面的变量 done 以检查它是否设置为 true。
警告此示例可能由于跨域脚本安全性而无法工作,只有当您 100% 两个页面都在同一个域上时才应使用此示例
子页面:
var done = false;
setTimeout(function () {
done = true;
}, 10000);
父页面:(脚本需要放置在 HTML 之后 / 正文标记 () 结束之前)
<div>
<div id="loading">
Loading...
</div>
<iframe id="iframeID"></iframe>
</div>
<script type="text/javascript">
var frame = document.getElementById('iframeID');
var loading = document.getElementById('loading');
frame.style.display = 'none'; //Originally hide the frame
loading.style.display = 'block'; //Originally show the Loading Screen
frame.src = 'Test2.aspx'; //Set the src attribute
frame.onload = function () {
console.log('Loaded Frame');
}
var $interval = setInterval(CheckFrameDone, 500);
function CheckFrameDone() {
var done = frame.contentWindow.done;
console.log(done);
if (done) {
console.log('Frame is Finished Loading');
frame.style.display = 'block';
loading.style.display = 'none';
clearInterval($interval);
} else {
console.log('Still Waiting...');
}
}
</script>
在第二个示例中,您会注意到每 500 毫秒父页面将检查子页面的 done 值,如果是 true,它将显示框架并清除间隔。否则它将继续检查。