Kyle Cureau
Kyle Cureau

Reputation: 19366

Fire event on last loaded iframe

How can I fire something on the last in a series of load events? I'm loading multiple iframes and need to fire a function when all the iframes have finished loading. So I need to bind the function to the last iframe loaded's load event.

Is this possible? I don't want to force synchronous loading if possible.

Upvotes: 3

Views: 589

Answers (1)

Jacob
Jacob

Reputation: 78840

A simpler way to solve the problem would be to attach the same load handler to all of the iframes, maintain a counter, then run your code once the required number of load events have taken place:

<iframe src="url1" onload="onFrameLoaded();"></iframe>
<iframe src="url2" onload="onFrameLoaded();"></iframe>
<iframe src="url3" onload="onFrameLoaded();"></iframe>

<script type="text/javascript">
    var loadedCount = 0;
    function onFrameLoaded() {
        loadedCount++;
        if (loadedCount >= 3) {
            // Do your thing
        }
    }
</script>

Upvotes: 7

Related Questions