Xander
Xander

Reputation: 9171

Iframe loaded in javascript

I dynamically am changing the location and src of an iframe.

Is there an event that will tell me when the page i just put has loaded from javascript?

Basically I want to append something to the page but first need to know that everything is loaded.

Upvotes: 1

Views: 192

Answers (2)

Hello71
Hello71

Reputation: 816

Something like this:


frame.src = url;
if (frame.addEventListener) {
    frame.addEventListener("load", function () {
        *insert code here*
    }, false);
} else if (frame.attachEvent) {
    frame.attachEvent("load", function () {
        *insert code here*
    }
}

Note that this code is not tested, and may not work.

BTW, onload, onclick, and such are outdated, mainly because they can only handle one event listener, unless you build your own (bad for productivity).

Upvotes: 1

Daniel Vassallo
Daniel Vassallo

Reputation: 344251

You may want to use the onLoad event, as in the following example:

<iframe src="http://www.google.com/" onLoad="alert('Test');"></iframe>

The alert will pop-up whenever the location within the iframe changes, including the first time it finishes loading. However note that this may not work with some older browsers like IE5 and early Opera. (Source)

Upvotes: 3

Related Questions