Print button in hard copy

2019-02-28 01:37发布

问题:

I have a printing script in javascript saved in this fiddle. But I unable to remove the print button from the hardcopy. Is there any other methods to remove the print button? The script is given below:

function printpage()
{
var data = 'Sample Report<br />Sample Report<br />Sample Report<br />';
    var data = data+'<br/><button onclick="window.print()"  class="noprint">Print the Report</button>';       
    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();
}

and html is

<input type="button" value="Print Preview" onclick="printpage()" />

回答1:

Add the style declaration to preview window:

function printpage() {
    var data = 'Sample Report<br />Sample Report<br />Sample Report<br />';
    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.body.innerHTML = data;
    myWindow.focus();
}

http://jsfiddle.net/4d3jj/2/