Javascript: Blank page when trying to document.print() after window.write()

Posted on 2008-11-14
Last Modified: 2012-05-05
I have a function that loads the content of a DIV into a new window. This is called with After the window loads, javascript calls the print() function.

The print() function executes before the window is finished loading the content. The print dialog comes up, but the new window is blank. Therefore, it prints a blank page. If hit 'cancel' on the print dialog, the screen loads.

What I need help with:
Can someone show me the correct way to check that the window content has finished loading before the WindowObject.print(); function is executed?

Thank you very much!!!


function tab1_printDiv(divID)


	var DocumentContainer = document.getElementById(divID);

	var WindowObject ="", "xxxxxxxxxxxxx", "width=800,height=605,top=40,left=40,toolbars=no,scrollbars=yes,status=no,resizable=yes");

	WindowObject.document.writeln('<link rel="stylesheet" href="css/chimneysupply1.css" type="text/css" media="screen, print" />');

	WindowObject.document.writeln('<link rel="stylesheet" href="css/tab1_printCapDiv.css" type="text/css" media="screen, print" />');







Open in new window

Question by:ChimneySupply
    LVL 21

    Expert Comment

    you could setTimeout(print(), 6000);

    Author Comment

    Hi Silemone -

    Thank you for your response!

    Unfortunately, the setTimeout is not a good solution because it does not take into consideration the actual time that it takes to load the new window content. The load time varies based on the users' connection speed. It seems that I need some way to check the new window content to make sure that the window.write has loaded all content before calling the print() function.
    LVL 21

    Expert Comment

    then more than likely you need to use ajax because it has different states like initialize, load...endload...etc...not exactly the right names, but it does know which execution is complete...
    LVL 21

    Accepted Solution

    another option is to:

    <body onsubmit="tab1_printDiv(divID); printAll();">

    doesn't have to be onsubmit event...on whatever event you using...

    Featured Post

    Highfive + Dolby Voice = No More Audio Complaints!

    Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

    Join & Write a Comment

    When you need to keep track of a simple list of numbers or strings, the Array object is your most direct tool.  As we saw in my earlier EE Article (, typical array handling might look like this: (CODE) B…
    The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
    The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
    The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

    733 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now