1

I have the following JavaScript code

frame.onload = function()
{
  // Get element
}
frame.src = url //url is an external and I don't have control over it

But here frame.onload event fires before the frame fully loaded. If I set timeout for 5 seconds after that I can get needed element. So how to wait frame to fully load?

Ashot Khachatryan
  • 2,156
  • 2
  • 14
  • 30
  • Use callbacks, like in this answer: http://stackoverflow.com/a/209723/640263 – Peter Dempsey May 03 '17 at 20:09
  • @toggy-tog-togs how? – Ashot Khachatryan May 03 '17 at 20:13
  • @toggy-tog-togs He is using a callback. – Barmar May 03 '17 at 20:36
  • @Barmar You are right, sorry. I created this small fiddle, and I can access the elements within perfectly fine (on load), https://jsfiddle.net/e6hgfrk6/. Could you possibly provide more of your code/detail so we can see what is going on? – Peter Dempsey May 03 '17 at 20:54
  • What kinds of elements are you having trouble accessing? Are they being loaded by Javascript? The `onload` event doesn't wait for Javascript to run, so it can't wait for those elements to load. – Barmar May 03 '17 at 20:59
  • Yes there could be elements that are being loaded by JavaScript. But I read about onload it says that it fires once a web page has completely loaded all content (including images, script files, CSS files, etc.). – Ashot Khachatryan May 03 '17 at 21:02
  • @AshotKhachatryan Any luck with a solution for this? – SQLiteNoob Jun 14 '18 at 13:39

1 Answers1

1

I think you need to use the load event of the document contained in the iframe, not the iframe itself.

frame.onload = function() {
    frame.contentDocument.onload = function () {
        // Get element
    };
};
frame.src = url;
Barmar
  • 741,623
  • 53
  • 500
  • 612