Solved

detecting when dynamically generated iframe has loaded

Posted on 2007-11-21
5
1,447 Views
Last Modified: 2011-10-03
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

0
Comment
Question by:drew22
5 Comments
 
LVL 21

Expert Comment

by:nizsmo
Comment Utility
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
 
LVL 82

Accepted Solution

by:
hielo earned 500 total points
Comment Utility
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
 
LVL 1

Expert Comment

by:alexgcm
Comment Utility
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
 
LVL 1

Author Comment

by:drew22
Comment Utility
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
 
LVL 1

Author Closing Comment

by:drew22
Comment Utility
iframe onload does just whatI'm looking for.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now