2

I have a page with a dynamic number of iframes. window.print() has to be called when iframes has loaded (ie. has src).

How can this be handled elegantly in pure JavaScript without being to pedantic?

2
  • Are the iframes all under your control? Or are some of them different domains that you do not control? Commented Oct 5, 2012 at 21:18
  • It's from an outside source and not under my control. Commented Oct 5, 2012 at 22:12

3 Answers 3

1
function getLoadedFrames () {

    var frames = document.getElementsByTagName( 'iframe' ), // Get all iframes
        loadedFrames = [], i;

    for ( i = 0; i < frames.length; i++ ) {
        /*
            If iframe has a src attribute, that attribute has a value AND
            that value is not equal to the current window location (leaving src
            blank seems to return the current location instead of empty string)
            then add the frame to the loadedFrames array.
        */
        if ( frames[ i ].src && frames[ i ].src.length > 0 && frames[ i ].src !== window.location.href ) {
            loadedFrames.push( frames[ i ] );
        }
    }

    return loadedFrames; // An array of your 'loaded' frames
}
Sign up to request clarification or add additional context in comments.

2 Comments

Heavy on the if conditions, but it's kind of a weird problem so this is a reasonable solution.
@kevin : is there a way to check if iframe has src or not ...........if(iframe_loaded){do_nothing...}else{load the iframe src}
0

Not quite sure if that's what you want, but perhaps it helps:

window.addEventListener("DOMContentLoaded", function(){
    // Get all iframes as soon as DOM has loaded
    var iframes = document.getElementsByTagName("iframe");
    for (var i = 0, l = iframes.length; i < l; ++i) {
        // Add event handler to every iframe
        iframes[i].onload = function(){
            // Execute window.print() when iframe has loaded
            window.print();
        };
    }
});

This works without checking the src attribute, because if a iframe doesn't have a src, the onload event will never be fired.

Note that this won't work in IE; see this answer on SO for an equivalent to jQuery's $(document).ready...

3 Comments

Thanks, but to support IE, it gets kind of pedantic ;-)
But in general cases, it's better checking the onload, as I'll have to add some additional loadtime is required for each iframe even though it has src.
Those were my thoughts too. Perhaps you should consider being a bit pedantic in this case... ;)
0

You could do

function areAllIframesLoaded() {
  var frames = document.getElementsByTagName( 'iframe' )
  return Array.prototype.slice.call(frames).reduce(function(p, c) {
    return p && c.src && c.src.length > 0 && c.src !== window.location.href
  }, true);
}

This will iterate over all the iframes and return true or false on whether or not all of them have a src defined.

3 Comments

frames.reduce returns an error in Fx 15? "Error: TypeError: frames.reduce is not a function"
Apparently not, but it is Firefox 15.0.1.
Durr, I needed to use Array.prototype.slice first. Fixed

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.