I have an iframe that changes src pages when the page would normally switch to another page so that my main JS file can continue running in the background. For the first page, the one defined in the HTML src for the Iframe, I can access the inner document just fine by using iframe.contentDocument.
However, when I change the src, the contentDocument does not change with it, and I cannot access the Iframe's document in the JS at all.
So is there any way to access the new document of an Iframe after changing its source in JS? Or is there an easier alternative for the way I am doing things? Thanks.
How I've tried:
iframe.src = "pages/home/home.html"
innerDoc = iframe.contentDocument || iframe.contentWindow.document
and the innerDoc does not change from the original page. I've even tried making an entirely new Iframe that never had the original page as its src
document.body.removeChild(iframe)
let i = document.createElement('iframe')
i.src='pages/home/home.html'
document.body.appendChild(i)
innerDoc = i.contentDocument || i.contentWindow.document
This just makes innerDoc an empty HTML document with an empty head and body.
i.contentWindow.addEventListener("load", ...)