0

Possible Duplicate:
Jquery: how to check if a newly opened window has been fully loaded? / window loads an external page (like www.yahoo.com)

So i have this function, that takes some html codes, and the it will open up a new window and place the code in the window, and then prompt the print function. However, sometimes, the print css doesn't get loaded, and the print doesn't build the proper page. Is there any way we can see if the newly opened window has loaded all the contents?

function printer(el)
    {
        print_window= window.open();

        var print_css = '<link rel="stylesheet" href="css/bootstrap.css" type="text/css" />'; 

        print_css = print_css + '<link rel="stylesheet" href="css/main.css" type="text/css" />';
        print_css = print_css + '<link rel="stylesheet" href="css/print.css" type="text/css" media="print" />';


        print_window.document.write("<html><head>" + print_css + "</head><body>" + "<div class='contents'>" + el + "</div>" + "</body> </html>");
        print_window.document.close();
        print_window.focus();
        print_window.print();
        print_window.close();

    }

Thanks

Community
  • 1
  • 1
  • 1
    does `print_window.onload` work? – Blazemonger Dec 03 '12 at 18:22
  • BTW, I see no jQuery in your code. Did you mean to add it as a tag? – Blazemonger Dec 03 '12 at 18:23
  • the print_window.onload did work, but for some reason it's not working well with the print function. When i tried giving an alert, it works, but for print, the newly opened window just stays there, with no prompts. –  Dec 03 '12 at 18:36
  • Sorry, I thought there would be solutions on jQuery, that's why I tagged jQuery. –  Dec 03 '12 at 18:37

1 Answers1

3

This might help -- use the window.onload event handler:

print_window.document.close();
print_window.onload = function() {
    print_window.focus();
    print_window.print();
    print_window.close();
};

As usual for window.onload, this event won't fire until all linked images, scripts, and stylesheets are loaded and ready.

http://jsfiddle.net/mblase75/3BEkV/

Blazemonger
  • 90,923
  • 26
  • 142
  • 180
  • Thank you, but for some reason it is not working as it should be. The print command is being fired from the main window, which I am trying to avoid because Chrome doesn't allow printing within another 30 seconds. And the newly opened window just stays there, not closed. I am testing on the latest version of the Chrome. –  Dec 03 '12 at 18:44