Link to home
Start Free TrialLog in
Avatar of Whing Dela Cruz
Whing Dela CruzFlag for Anguilla

asked on

Div print

Hi Experts, I'm using the code below to print data in div. However, I noticed that the approached would open other window and that be able to show other page to print or to save. My problem is that I could no longer run my web site once this window will not close. This Untitled page would stop me from re-running my site. Is there anyone who can explain me why this thing is happen? What's the solution to prevent this problem? Thanks!

function printData(id){
   var divToPrint = document.getElementById('Prolist-A34');
   newWin= window.open();
   newWin.document.write(divToPrint.outerHTML);
   newWin.print();
   newWin.close();  
}

Open in new window

Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

I am unable to replicate the problem.

If I run your code a window opens - the document prints, the window closes and I am back to the original page - I can repeat this as often as I want.

I am not sure what you mean by you "can no longer run your website" - we probably need to see more code or a link.

Having said that

Why not just create a style sheet with media="print" and hide the elements you don't want in your printout.

You specify the stylesheet like this

<link rel="stylesheet" media="print" href="print.css">

Open in new window


In the stylesheet you use classes to hide those elements you don't want visible when you print.
Avatar of Whing Dela Cruz

ASKER

Hi Julian, In my case when i run the code and open for printing the page it will works fine. However, when I'm back the original page without closing the window of  printing and refresh the original page, that's the problem. I can't refresh the original page while the two pages are open. Is there a way to prevent this?
I suspect the reason you can't refresh is because there is a print in progress. The newWin.print() is loading a modal print dialog that needs to be closed before you can do anything in the original window.
Hi Julian, Do you have some example of media="print"? Can you give some ideas how to start with? Thank you!
ASKER CERTIFIED SOLUTION
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you sir!
You are welcome.