Reputation: 1055
So I create an iframe and it's supposed to continue reloading after a function runs. The function reads info that is in the iframe. Currently I have something like this (Which works);
function loaded(){
alert(iframe.contentDocument.getElementsByClassName("blah")[0].innerHTML);
iframe.src = filePath; //reloads the iframe
}
iframe.onload = loaded;
As an alternative, would something like this work, where the function runs as soon as the iframe has loaded the DOM;
function loaded(){
alert(iframe.contentDocument.getElementsByClassName("blah")[0].innerHTML);
iframe.src = filePath; //reloads the iframe
}
iframe.addEventListener("DOMContentLoaded", loaded, false);
Upvotes: 1
Views: 1489
Reputation: 1690
I believe what you need is a DomContentLoaded event (check out your chrome dev tools for a blue line :) )
You can listen for this event like this
window.addEventListener('DOMContentLoaded', loaded, false);
Its unclear though whether you want to access frame window object from inside the frame or outside... if from outside ... you need window.frames['SOME_FRAME_ID']
and document.getElementById('SOME_FRAME_ID').contentWindow
for firefox to access frame window object.
For IE use fallback
window.attachEvent('onload', loaded);
And remember when working with frames - something will go horribly wrong
EDIT UPDATE
Suppose you have a frame
<iframe src="" id="myFavouriteFrame" ...>
Here goes some code
var iframeId = 'myFavouriteFrame';
var iframeWindow = null;
if(window.all && !window.opera) {
iframeWindow = window.frames[iframeId];
}
else {
iframeWindow = document.getElementById(iframeId).contentWindow; //Note - not standard
}
if(iframeWindow && iframeWindow.addEventListener) {
iframeWindow.addEventListener('DOMContentLoaded', load, false);
}
else if (iframeWindow && iframeWindow.attachEvent){
iframeWindow.attachEvent('onload', load);
}
Upvotes: 0
Reputation: 4170
If I understood you correctly (iframe is on the same domain and you can edit its content), you can add this in your iframe:
<script type="text/javascript">
//<![CDATA[
fireOnReadyEvent();
parent.loaded();
//]]>
</script>
This will call the loaded function in the parent page once the iframe is ready. And you can remove iframe.onload = loaded;
from the parent page.
Upvotes: 1