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

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

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

you could setTimeout(print(), 6000);
ChimneySupplyAuthor Commented:
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.
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...
another option is to:

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

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.