我想出了以下解決方案 - 因爲我們控制iframe中的內容與主窗口
我們添加下面的腳本來頁腳中的iframe中的內容(所有頁面使用相同的這是唯一可能模板,所以這是一個改變一個單一的文件)
<script>
window.onunload = function() {
// Notify top window of the unload event
window.top.postMessage('iframe_change', '*');
};
</script>
裏面的主窗口,我們添加這個腳本來監視iframe的狀態
function init_content_monitor() {
var content = jQuery('.iframe');
// The user did navigate away from the currently displayed iframe page. Show an animation
var content_start_loading = function() {
alert ('NOW: show the animation');
}
// the iframe is done loading a new page. Hide the animation again
var content_finished_loading = function() {
alert ('DONE: hide the animation');
}
// Listen to messages sent from the content iframe
var receiveMessage = function receiveMessage(e){
var url = window.location.href,
url_parts = url.split("/"),
allowed = url_parts[0] + "//" + url_parts[2];
// Only react to messages from same domain as current document
if (e.origin !== allowed) return;
// Handle the message
switch (e.data) {
case 'iframe_change': content_start_loading(); break;
}
};
window.addEventListener("message", receiveMessage, false);
// This will be triggered when the iframe is completely loaded
content.on('load', content_finished_loading);
}
也許這可以幫助:?IFRAME SRC變化事件檢測] http://stackoverflow.com/a/4010296/1427878 – CBroe
的可能的複製(https://stackoverflow.com/questions/2429045/iframe-src-change-event-detection) –