iframe默認加載src屬性的url,可是若是iframe內部發生跳轉,當前url並不會同步到src屬性上跨域
iframe每次從新加載頁面時都會觸發onload方法markdown
iframe.onload = function (e) {
//
}
複製代碼
使用MutationObserver監聽iframe的src屬性變化,需注意的是若是是iframe內部執行的方法致使url變化則不會觸發src屬性變化url
var MutationObserver = window.MutationObserver || window.WebKitMutationObserver || window.MozMutationObserver;
var observer = new MutationObserver(items => {
items.forEach(item =>{
console.error(item)
iframeChange(item.oldValue, item.target.src, item.target)
})
})
var options = {
attributes: true, // 屬性
attributeFilter: ["src"] // 表示須要觀察的屬性
}
observer.observe(Iframe, options);
function iframeChange(oldValue, newValue, iframeData){
console.log("舊地址:"+oldValue)
console.log("新地址:"+newValue)
}
複製代碼
經過contentWindow獲取spa
iframe.contentWindow.location.href
複製代碼
可是對於iframe和頁面有跨域的 , 是沒法獲取到contentWindow.location.href的code
Iframe.contentWindow.loadTimeData && SSOIframe.contentWindow.loadTimeData.data_.summary.failedUrl
或
window.document.querySelector('#reload-button').url
複製代碼