Avatar of chsalvia
chsalvia

asked on 

Printer-Friendly PopUp Window

I have a page that displays a table with a lot of stats.  I want to create a print button which will let the user print the stats without having to print the rest of the page.

It seems the best thing to do would be to create a pop-up window which generates a more printer-friendly version of the stats, and then calls the window.print() function.

Creating a popup window is somewhat problematic however.  When I write to the new window using document.write(), the pop-up window appears but starts loading endlessly on Firefox.  On IE this doesn't happen.  Secondly, trying to print the new window doesn't work at all on IE, but works on FF.

Here's the code:

function printWindow() {
      newwindow=window.open(url,'name','height=200,width=150');
      if (window.focus) {newwindow.focus()}
      newwindow.document.write("<h1>Test</h1>");
      newwindow.print();
}

Is there a way to get consistent cross-browser behavior here?
JavaScript

Avatar of undefined
Last Comment
Michel Plungjan
Avatar of Nushi
Nushi

u need to use CSS to accomplish that.
I will paste a sample in few minutes.
you open a pop up and set the CSS using medai and create CSS for printing.
SOLUTION
Avatar of Nushi
Nushi

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
ASKER CERTIFIED SOLUTION
Avatar of Michel Plungjan
Michel Plungjan
Flag of Denmark image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
Avatar of Nushi
Nushi

it should be:

newwindow.close();

Nushi
Avatar of Nushi
Nushi

and be aware that on IE:
window.print()"  - calling ActiveX so if user has blocked activeX it will not work.

Nushi.

No it should NOT be newwindow.close()
it MUST be
 newwindow.document.close();
to FINALISE the newwindow.document.write()

It is mandatory on many browsers to do this to tell the browser you finished your document.writes

Michel
Avatar of Nushi
Nushi

so it should be:

newwindow.document.close();
newwindow.close();


Nushi.
Nonono... You cannot close the window until after the print dialog has been shown

You can change to
 newwindow.document.write('<bo'+'dy onLoad="window.focus(); window.print()">'+
'<style media="print">.noPrint { display:none }</style>'+
document.getElementById(divId).innerHTML+
'<hr class="noPrint"><a class="noPrint" href="#" onClick="window.close(); return false">Close</a>'+
'</body>');
 





JavaScript
JavaScript

JavaScript is a dynamic, object-based language commonly used for client-side scripting in web browsers. Recently, server side JavaScript frameworks have also emerged. JavaScript runs on nearly every operating system and in almost every mainstream web browser.

127K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo