views:

99

answers:

3

I have a print button with id="print_req". I wrote some Javascript code for printing a page, which is triggered by clicking on this button, and I also want to hide this button before printing, and show it after whole printing process.i mean not to print button in my printed document. Here is my code:

$(document).ready(function(){
    $("#print_req").click(function(){
        $("#print_req").css("display","none");
        window.print();  
    });

    $("#print_req").css("display","block");
    return false;   
});

This correctly hide that button, but when the printing process get done, the button won't show again! What is the problem?

+2  A: 

The problem is that you cannot know when the printing is finished using javascript. There's no event that will be triggered to notify this.

For security reasons javascript is executed in a sandboxed environment inside the browser limiting its access to any system resources.

Darin Dimitrov
so how can i do this buddy?
armin etemadi
You can't using pure javascript. There are ActiveX controls which allows managing the printing but they work only on IE and the user should explicitly accept to install them.
Darin Dimitrov
oh!! i found my own answer!!! if i put the image of this button as a background of <a> html tag,it won't send to printer automatically! :)
armin etemadi
@armin, that's great and useful to other people having the same problem. You should have explained in your question that what you need is to exclude the print button when the page is printed.
Darin Dimitrov
i know it and i did it buddy.if my question is Ambiguous please tell me :)
armin etemadi
This is a bad answer when there is a better way to do it. Backgrounds will show up if the user has them enabled to print in their browser settings. And ActiveX is horrible. :)
epascarello
A: 

The print dialog box is part of the operating system. Javascript can only access items that are part of the web page - anything else would be a major security breach.

baklap
thank you,but how can i do this thing?
armin etemadi
+3  A: 

You are going at this wrong. You should not be showing and hiding the button with JavaScript or using a background image to do it. You should be using a print stylesheet that will allow you to apply different styles to the page when it is printed. In your case you would set the display to none [or visibility to hidden] in this stylesheet.

So you add a stylesheet with the media type for print

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

In that print.css, you hide the button

.printButtonClass{ display : none }

And presto, the button hides when you print with no JavaScript.

epascarello
your right,but i print a div that is in a page that button exists!i mean every style on printing box will affect the button on load time! for example : if i do your suggestion,it will hide the button at early time!
armin etemadi
and also i don't want to do such a complex job for doing this simple thing.but thank you buddy
armin etemadi
This will not hide the button until the page is printed or shown in print preview. It only effects the style of the button that you put a class on. It will not effect the page in anyway. Your solution of setting it to the image also adds more problems with accessibility and what if user's have images disabled? Will they know to print with that button? I find print button's silly anyway since there is one built into my browser.
epascarello
@armin No, notice the `media="print"` in the above example. That means these styles will only be used for the printed version of a page, they will be ignored (and the button will be visible) when the page is on screen
Gareth
oh,great.. :) i learned so much from your explanations.thank you so much buddy,it really helped me.yeah,your answer is better than mine,i will do your offer:)
armin etemadi