1

I have a sample fiddle here where I have a print preview button. On click of that button, it opens a new popup window which is created as,

function printpage() {
    var data = '<table border="1" cellspacing="0"><tr><td colspan="4">Sample Report</td></tr>' + document.getElementsByTagName('table')[0].innerHTML + '</table>';
    data += '<br/><button onclick="window.print()"  class="noprint">Print the Report</button>';
    data += '<style type="text/css" media="print"> .noprint {visibility: hidden;} </style>';
    myWindow = window.open('', '', 'width=800,height=600');
    myWindow.innerWidth = screen.width;
    myWindow.innerHeight = screen.height;
    myWindow.screenX = 0;
    myWindow.screenY = 0;
    myWindow.document.write(data);
    myWindow.focus();
}

When viewing its source using google chrome developer tools, I can get the following codes.

<html>
<head></head>
<body>
<table border="1" cellspacing="0"><tbody><tr><td colspan="4">Sample Report</td></tr>
    </tbody>
    <tbody>
    <tr><th>Sl.No</th><th>Value 1</th><th>Value 2</th><th>Value 3</th></tr>
    <tr><td>1</td><td>5</td><td>0</td><td>2</td></tr>
    <tr><td>2</td><td>7</td><td>0</td><td>4</td></tr>
    <tr><td>3</td><td>1</td><td>0</td><td>10</td></tr>
    <tr><td>4</td><td>8</td><td>0</td><td>3</td></tr>
    <tr><td>5</td><td>13</td><td>0</td><td>6</td></tr>
</tbody></table><br>
<button onclick="window.print()" class="noprint">Print the Report</button><style type="text/css" media="print"> .noprint {visibility: hidden;} </style>
</body>
</html>

Is it possible to add some extra javascript/jquery codes inside that pop up window?

2
  • 1
    Just add your script to the data. Here's an example at jsFiddle. Notice the escaped / in the ending script tag. Commented Feb 5, 2014 at 15:05
  • Thanks @Teemu.. Add it as a answer so that I can accept it. Commented Feb 6, 2014 at 4:13

1 Answer 1

1

You can add a script to data variable like so:

var data = '<script>/* CODE */ <\/script>' +
    '<table ...>' +
           :
    '<style...>';

The ending script tag within a string must somehow be broken, so that it doesn't appear as a literal </script>. If it does, the script will break, when the tag is found.

Also you need to take care, that quotes within a script string are not breaking the string. Use "s and escape ' with a backslash when needed.

Actually there are more elegant ways to style a page (or part of it) for printing, you could use for example Media Queries, or link (see media) a separate stylesheet to style prints.

Sign up to request clarification or add additional context in comments.

2 Comments

I added some script here to my sample, but not working. Any fault in my script?
Your print window never fire $(document).ready(...), since you haven't closed its document after write(). You need to put myWindow.document.close(); just after document.write(). The code is working fine when you close the document too.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.