JavaScript - how to give print command using javascript

Asked By theiry henry on 10-Sep-09 01:29 AM
how to giv print command to web page for printing along with stylesheet in javascript?

print in javascript

Venkat K replied to theiry henry on 10-Sep-09 01:35 AM

Function to print the present window in javascript

<SCRIPT LANGUAGE="JavaScript">
function myprint()
{
window.print();
}
//  End -->
</script>

you can assign the above javascript function in onClick on a HTML Button:

<form>
<input type=button value="Print Page" onClick="myprint()">
</form>

Thanks,

Vasanthakumar D replied to theiry henry on 10-Sep-09 01:38 AM

Hi,

use

window.print();

window.print() is not including d style sheet with it .Is der any other method to do so?

theiry henry replied to Vasanthakumar D on 10-Sep-09 02:13 AM
window.print() is not including d style sheet with it .Is der any other method to do so?
how to give print command using javascript
mv ark replied to theiry henry on 10-Sep-09 02:14 AM
Note that you can use separate stylesheets for displaying content on the screen and while printing the same page  -
<link rel="stylesheet" type="text/css" href="path/to/screen.css" media="screen" />
<link rel="stylesheet" type="text/css" href="path/to/print.css" media="print" />

The above 2 lines mean that when the page is requested from a web browser, the screen.css file will dictate the style of the page(s) and when printed or print previewed, the print.css file will dictate the style of the page.

When you use window.print() to print the page, the style definitions in print.css will take effect.
CSS dont get applied to gridview
theiry henry replied to mv ark on 10-Sep-09 03:18 AM
i hav grid in that page and i hav applied css to grid but i cant see it in print preview.
Take a Look
Vishal Chourasiya replied to theiry henry on 15-Sep-09 02:05 AM

<script type="text/javascript" language="javascript">

function PrintPage()

{

      window.print();

}

</script>

<asp:Button id="btnPrint" runat="server" text="Print" onClientClick="PrintPage(); return false;"/>

and you have done.