I have created a iframe dynamically and added a src
attribute to it. Then I have appended this iframe to body of the page. Know I want to attach an onload
You can add a load event listener to the iframe's contentWindow, which is just like any other Window.
Here's an example:
iframe = document.createElement('iframe')
iframe.setAttribute('src', 'https://example.com/')
document.getElementsByTagName('body')[0].appendChild(iframe)
const onLoaded = function() {
console.log('iframe loaded');
}
if (iframe.contentWindow.document.readyState === 'complete') {
console.log('already loaded')
onLoaded()
} else {
iframe.contentWindow.addEventListener('load', onLoaded)
}