Java J2ME JSP J2EE Servlet Android

Print a part of a webpage using javascript

We can easily print a part of a webpage by calling window.print() .

Here is an example to print a <div>


 

<html>

<head>

</head>

<body>

    <div style = "width:100%;height:100" id="myDiv">

        This is to print<br/>

        Testing

    </div>

    <div style = "width:100%;height:100">

        This is not to print<br/>

        Testing

    </div>

    <br/>

    <span onClick="printIt();">Click here</span>

<script type = "text/javascript">

function printIt(){

    user_pref("print.print_footercenter","");

user_pref("print.print_footerleft","");

user_pref("print.print_footerright","");

user_pref("print.print_headerleft","");

user_pref("print.print_headercenter","");

user_pref("print.print_headerright","");

    wi = window.open('', 'p');

    wi.document.open();

    element=document.getElementById("myDiv");

    wi.document.write(element.innerHTML);

    wi.print();

    wi.document.close();

    wi.close();

}

</script>

</body>

</html>