最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Remove print button from hard copy - Stack Overflow

programmeradmin1浏览0评论

With reference to this link and this, I printing a report using javascript as

 <!DOCTYPE html>
<html>
<head>
<script>
function printpage()
{
var data = 'Sample Report<br />Sample Report<br />Sample Report<br />';
    var data = data+'<br/><button onclick="window.print()">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();
}
</script>
</head>
<body>

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

</body>
</html>

But after printing, the print button still remains at the hard copy. So how to hide the print button in hard copy when printing by using the above function?

With reference to this link and this, I printing a report using javascript as

 <!DOCTYPE html>
<html>
<head>
<script>
function printpage()
{
var data = 'Sample Report<br />Sample Report<br />Sample Report<br />';
    var data = data+'<br/><button onclick="window.print()">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();
}
</script>
</head>
<body>

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

</body>
</html>

But after printing, the print button still remains at the hard copy. So how to hide the print button in hard copy when printing by using the above function?

Share Improve this question edited Jun 6, 2013 at 12:00 mpsbhat asked Jun 6, 2013 at 11:19 mpsbhatmpsbhat 2,77312 gold badges51 silver badges107 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 7

Give your button a class, e.g. class="noprint". Then Add a stylesheet for print media to your CSS:

@media print {
  /* style sheet for print goes here */
  .noprint {
    visibility: hidden;
  }
}

Details: http://www.w3/TR/CSS2/media.html

I just solved this with the following css after assigning id to print button.

<style type="text/css">
@media print {
    #myPrntbtn {
        display :  none;
    }
}
</style>

And my button as follows.

<input id ="myPrntbtn" type="button" value="Print" onclick="window.print();" >

Also, you can run javascript function with the following actions...

  1. Hide the button
  2. Print the page using window.print();
  3. Again Show back the button

Piece of code...

<script type="text/javascript">
    function printMyPage() {
        //Get the print button
        var printButton = document.getElementById("myPrntbtn");
        //Hide the print button 
        printButton.style.visibility = 'hidden';
        //Print the page content
        window.print()
        //Show back the print button on web page 
        printButton.style.visibility = 'visible';
    }
</script>

Reference Hide Print button while printing a web page

发布评论

评论列表(0)

  1. 暂无评论