Tech or Treat! Write an article about your scariest tech disaster to win gadgets!Learn more

x
?
Solved

detecting when dynamically generated iframe has loaded

Posted on 2007-11-21
5
Medium Priority
?
1,456 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 2000 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
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…

647 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