I have created a iframe dynamicaly 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 event to iframe to read the iframe content. Can somebody suggest how do i do that.
frame = document.createElement('iframe');
frame.setAttribute('src','http://myurl.com');
body.appendChild(frame);
frame.onload = function(){
alert('hi'); // here i want to read the content in the frame.
}
Some browsers do have the onload event for an iframe, first you should try to attach it before setting the iframe's src attribute.
I'd avoid using it altogether since in certain browsers it might not fire under certain conditions (e.g. the target was in cache in IE).
You could user a timer to check if the frame's contentWindow's readystate is complete
I doubt you can attach an onload event to an iframe. It will not work on all browsers. You can on the other hand check if the iframe was loaded by:
Or use AJAX to load the content in your container. With AJAX you can set a proper load-complete event.
I just tried this and it worked in Chrome:
W3school says
contentWindow
is supported in all major browsers.