Can you have call javascript only when print is called?

百般思念 提交于 2019-12-24 06:47:50

问题


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>

回答1:


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




回答2:


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 ....




回答3:


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..



来源:https://stackoverflow.com/questions/7726252/can-you-have-call-javascript-only-when-print-is-called

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!