Dennis Martinez
Dennis Martinez

Reputation: 6512

Can you have call javascript only when print is called?

I need to only use a piece of javascript when the print button is clicked, is there a way to do this?

EDIT: A browser print button, i was thinking something like this:

<script type="text/javascript" src="path" media="print"></script>

Upvotes: 1

Views: 2807

Answers (3)

Manse
Manse

Reputation: 38147

Some browsers (Firefox and IE) have events onBeforePrint onAfterPrint ...

https://developer.mozilla.org/en/Printing#Detecting_print_requests

Failing that use a "Press this to print" button and perform what you want from there ....

Upvotes: 1

Jan Pfeifer
Jan Pfeifer

Reputation: 2861

There is only a partial support from browsers for that. Consider using special CSS stylesheet for print version of the page. For more details look at this question (there are links to several others): execute javascript when printing page

Upvotes: 2

Rob
Rob

Reputation: 4947

I think you mean this: JQuery:

$('#btnprint').click(function(){
     //Your JS code goes here
});

Watch out with postbacks!

Im not sure wether u mean the browsers print button or just a custom one, the code above is for your own custom one..

Upvotes: 0

Related Questions