Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2431
  • Last Modified:

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 window.open(). After the window loads, javascript calls the print() function.

Problem:
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!!!

=)

Mike
function tab1_printDiv(divID)
{
	var DocumentContainer = document.getElementById(divID);
	var WindowObject = window.open("http://www.xxxxxxxxxx.com", "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" />');
	WindowObject.document.writeln(DocumentContainer.innerHTML);
        WindowObject.document.close();
	WindowObject.focus();
	WindowObject.print();
	WindowObject.close();	
}

Open in new window

0
ChimneySupply
Asked:
ChimneySupply
  • 3
1 Solution
 
silemoneCommented:
you could setTimeout(print(), 6000);
0
 
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.
0
 
silemoneCommented:
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...
0
 
silemoneCommented:
another option is to:

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


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

Featured Post

Become an Android App Developer

Ready to kick start your career in 2018? Learn how to build an Android app in January’s Course of the Month and open the door to new opportunities.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now