您可以使用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
,它將顯示幀並清除間隔。否則它將繼續檢查。
你的答案部分正確。我也需要動態加載的資源來通知父母。舉一個例子,我需要確定瀏覽器類型並相應地加載mp3/ogg,這可以通過javascript完成。因此,我需要處理iframe中的加載處理程序並加載動態資源,如音頻文件,然後才發送加載的信號。 – hevi
當你說'在iframe中'你只是指iframe鏈接到的頁面?如果是這樣,你可以嘗試在父頁面上運行一個'setInterval'來檢查子變量是否被設置爲true,也就是說,只有在完成加載和完成時,Child設置爲'var done = true;' == true'顯示框架。 – Nunners
@hevi請參閱我的編輯答案與新的例子。 – Nunners