detecting when dynamically generated iframe has loaded

I need to find out when the iframe in bookmarklet script below has loaded, so i can access the DOM in it. As it is now, when I try to access the iframe DOM, it doesn't work because the iframe is still loading its src.

The reason I'm doing this is that the page I'm trying to scrape has an unloaded ajax tab, so I'm grabbing the  ajax url and using it as the src property of the dynamically generated iframe. Then i scrape the content of the iframe
var thebody = document.getElementsByTagName('body')[0];
 
var my_iframe = document.createElement('iframe');
my_iframe.setAttribute("id","frame1");
my_iframe.setAttribute("name","frame1");
my_iframe.src = 'http://asite.com/apage.php';
thebody.appendChild(my_iframe);
 
// access the iframe DOM

Open in new window

LVL 1
drew22Asked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
hieloConnect With a Mentor Commented:
There is no reliable way to know this. You can for instance modify your code to:
function done(){
alert("It Loaded");
}
my_iframe.setAttribute("name","frame1");
my_iframe.onload=done;
my_iframe.src = 'http://asite.com/apage.php';
but:
1. It does not work on all browsers. Last time I checked it worked only on  some particular version of IE, but don't remeber the version.
2. It simply tells you if there is "some" new content on the frame, as opposed to "the requested content"

To clarify, if you were to mistype the url, the server will send you "Error 404: Page Not found" page/content, but the onload will still be triggered.

Not too long ago someone asked a similar question. My detailed responses, some of which may be work arounds for your problem(my first post in particular) are found here:
http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_22906456.html
0
 
nizsmoDeveloperCommented:
hi drew22:

You can use a timeout function before accessing the iframe dom, making sure that it has already been loaded.

setTimeout("WHAT_YOU_WANT_TO_DO_HERE",2000)

this gives a 2 second timeout, you can adjust accrodingly.

hope this helps.
0
 
alexgcmCommented:
You can only work with iframes like that if the iframe src domain is the same as you document URL.

One way to do that, is to put a little script at the page inside the iframe(body onload), that calls a function like ILoaded() from the upper document.


But if your iframe is at one domain, and your document(the iframe creator) is at another domain(ie.: http://www.google.com is the creator and http://microsoft.com is the page inside the iframe), neither the parent document can access iframe DOM or  the iframe can access the parent DOM.
0
 
drew22Author Commented:
great suggestions, the my_iframe.onload and inserting script. I'll try it out. if this works on only one browser it will be fine; that's the browser i'll use.
0
 
drew22Author Commented:
iframe onload does just whatI'm looking for.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.