Print This Page
This JavaScript function is very useful where we have "Print this page" button.
Example: Print your invoice
Conventionally, the logic for implementing the functionality is:
1. Whole of the page is displayed [with a button(Print) on top/bottom ]
2. This generated page could be a Static HTML page, or a Dynamic page(Created in scripting language like PHP, ASP, C#.net etc).
3. On clicking on "Print" button, a new window(or pop-up) is opened, where full code for generating the same HTML (for required part of page) is written again.
4. Then page is printed.
With this function, You just need to:
1. Have all the required part (needed to print) into a DIV tag.
2. OnClick of the "Print" Button call this function & pass the ID of DIV tag.
3. It's done :)
function PrintThisPage(print_div_id)
{
var popup_content = document.getElementById(print_div_id).innerHTML;
var sOption="toolbar=yes,location=no,directories=yes,menubar=yes,";
sOption+="scrollbars=yes,width=620,height=460,left=100,top=25";
var winprint=window.open("","",sOption);
winprint.document.open();
winprint.document.write(popup_content);
winprint.print();
winprint.document.close();
winprint.focus();
}
This is tested in Mozilla FireFox, Internet Explorer.
Hopefully it will work in all other browsers too.
No comments:
Post a Comment