-3

This question is mostly out of academic interests(and partly because I faced an issue like this before and simply found a workaround, but I want to know the proper way to do it).

How does the window.print() JavaScript function decide which parts of a page to print? My end goal is to add new fields to the 'print' dialog which is not there in the original page and also be able to prevent some content in my page from being printed(for eg: prevent some plaintext links on my webpage being printed onto paper).

Can I edit the function to suit my requirements? Where are the JS files located?

Ajay
  • 11
  • 4
  • "The print() method prints the contents of the current window." If you want it to print the special data - open this data in a new window and use print() method on it. – Yeldar Kurmangaliyev Jun 02 '15 at 06:03
  • `window.print` not create the *dialog*, which create the *dialog* is the web-browser, `window.print` only triggers a request to the browser. You will not get more than with `window.print`. – Protomen Jun 02 '15 at 06:10
  • No need to explain. And this question will be indexed and become a signpost to the canonical answer, which may help people in your situation in the future. –  Jun 02 '15 at 16:54

3 Answers3

1

window.print() function prints the whole page. Using css media queries you can control what you want to print/do not print for print devices.

Learn more from here - http://www.joshuawinn.com/css-print-media-query/

HTML Code:

<a class="text_link">Link text not to be printed.</a>

CSS Code:

@media print { 
 .text_link{display:none} // this will hide the text link and hence will not be printed.
}
Ashwin
  • 12,081
  • 22
  • 83
  • 117
0

Yes, add a class to the elements you don't want to print and in your style tag use the media="print"

then adjust accordingly and you will have what you want.

<style type="text/css" media="print">
    .elementYoudontWanttoPrint {
         display:none;
    }
</style>
1''
  • 324
  • 1
  • 10
-1

You need to add a css file with media specified as a print. In this CSS file you can write different classes in which you can easily set elements' visibility using css attributes.

http://www.w3schools.com/css/css_mediatypes.asp http://www.smashingmagazine.com/2011/11/24/how-to-set-up-a-print-style-sheet/

prashant
  • 2,181
  • 2
  • 22
  • 37
  • Ok. Please have courtesy to add a comment before marking it as this answer is not useful. Just marking it wont help. – prashant Jun 02 '15 at 06:18
  • Your answer was actually quite helpful. I was not the one who -1'd it , but I dont have enough rep to actually upvote you or anyone else, so Thank you for the effort! :) – Ajay Jun 02 '15 at 06:30