Solved

detecting when dynamically generated iframe has loaded

Posted on 2007-11-21
5
1,448 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
ID: 20330951
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
ID: 20331193
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
ID: 20331197
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
ID: 20331895
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
ID: 31410454
iframe onload does just whatI'm looking for.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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…

914 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

16 Experts available now in Live!

Get 1:1 Help Now