简体   繁体   中英

Load iFrame and print it

I have an ASPX page with hidden iframe.
I'm trying to: on button click load another page into that hidden iFrame and print it's content:

$("#shareButtonPrint").click(function () {
        $("#PrintFrame").attr('src', '/ProtocolPrintPage.aspx?g=1');
        $("#PrintFrame").ready(function () {
            window.frames['PrintFrame'].print();
        });

    });

but the code above doesn't work.

It works only when I load the iframe on main page load:

$("#PrintFrame").attr('src', '/ProtocolPrintPage.aspx?g=1');
    $("#shareButtonPrint").click(function () {
        //$("#PrintFrame").attr('src', '/ProtocolPrintPage.aspx?g=1');
        window.frames['PrintFrame'].print();
    });

How can I load the iframe on button click ?

This is likely due to the iframe not yet being loaded when you're trying to print it

Perhaps try using load instead of ready

Check out my example here

http://jsfiddle.net/andyw_/umYkV/451/

Only tested in latest chrome + IE8-10.

Also note that load is deprecated

http://api.jquery.com/category/deprecated/

The new way of doing it is on('load', fn)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM