Exactly when does an IFRAME onload event fire?

后端 未结 3 1194
逝去的感伤
逝去的感伤 2021-02-12 11:42

Does the IFRAME\'s onload event fire when the HTML has fully downloaded, or only when all dependent elements load as well? (css/js/img)

相关标签:
3条回答
  • 2021-02-12 12:02

    Just when the html loads, not the dependent elements. (or so I think).

    To fire when the rest of the page loads do jQuery(window).load(function(){ or window.onload not document onready.

    You can also check to see if an image element is loaded and there... if image . load-- etc.

    0 讨论(0)
  • 2021-02-12 12:20

    The above answer (using onload event) is correct, however in certain cases this seems to misbehave. Especially when dynamically generating a print template for web-content.

    I try to print certain contents of a page by creating a dynamic iframe and printing it. If it contains images i cant get it to fire when the images are loaded. It always fires too soon when the images are still loading resulting in a incomplete print:

    function printElement(jqElement){
        if (!$("#printframe").length){
            $("body").append('<iframe id="printframe" name="printframe" style="height: 0px; width: 0px; position: absolute" />');
        }
    
        var printframe = $("#printframe")[0].contentWindow;
        printframe.document.open();
        printframe.document.write('<html><head></head><body onload="window.focus(); window.print()">');
        printframe.document.write(jqElement[0].innerHTML);
        printframe.document.write('</body></html>');
    //  printframe.document.body.onload = function(){
    //      printframe.focus();
    //      printframe.print();
    //  };
        printframe.document.close();
    //  printframe.focus();
    //  printframe.print();
    
    //  printframe.document.body.onload = function()...
    }
    

    as you can see i tried out several methods to bind the onload handler... in any case it will fire too early. I know that because the browser print preview (google chrome) contains broken images. When I cancel the print and call that function again (images are now cached) the print preview is fine.

    ... fortunately i found a solution. not pretty but suitable. What it does that it scans the subtree for 'img' tags and checking the 'complete' state of those. if uncomplete it delays a recheck after 250ms.

    function delayUntilImgComplete(element, func){
        var images = element.find('img');
        var complete = true;
        $.each(images, function(index, image){
            if (!image.complete) complete = false;
        });
        if (complete) func();
        else setTimeout(function(){
            delayUntilImgComplete(element, func);}
        , 250);
    }    
    
    function printElement(jqElement){
        delayUntilImgComplete(jqElement, function(){
            if (!$("#printframe").length){
                $("body").append('<iframe id="printframe" name="printframe" style="height: 0px; width: 0px; position: absolute" />');
            }
    
            var printframe = $("#printframe")[0].contentWindow;
            printframe.document.open();
            printframe.document.write(jqElement[0].innerHTML);
            printframe.document.close();
            printframe.focus();
            printframe.print();
        });
    }
    
    0 讨论(0)
  • 2021-02-12 12:22

    The latter: <body onload= fires only when all dependent elements (css/js/img) have been loaded as well.

    If you want to run JavaScript code when the HTML has been loaded, do this at the end of your HTML:

    <script>alert('HTML loaded.')</script></body></html>
    

    Here is a relevant e-mail thread about the difference between load and ready (jQuery supports both).

    0 讨论(0)
提交回复
热议问题