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
复制代码