Thursday, October 11, 2018

print div in javascript , print div content using jquery

print div in javascript , print div content using jquery



javascript

<html>
<head>
<title>Print div content using jquery</title>
<script type="text/javascript">
        function PrintDiv() {
            var contents = document.getElementById("dvContents").innerHTML;
            var frame1 = document.createElement('iframe');
            frame1.name = "frame1";
            frame1.style.position = "absolute";
            frame1.style.top = "-1000000px";
            document.body.appendChild(frame1);
            var frameDoc = frame1.contentWindow ? frame1.contentWindow : frame1.contentDocument.document ? frame1.contentDocument.document : frame1.contentDocument;
            frameDoc.document.open();
            frameDoc.document.write('<html><head>');
            frameDoc.document.write('</head><body>');
            //Append the external CSS file.
            //frameDoc.document.write('<link href="css/font-awesome.css" rel="stylesheet">');
            frameDoc.document.write('<link href="css/style.css" rel="stylesheet" type="text/css" />');
            frameDoc.document.write('<link href="css/tabletrtd.css" rel="stylesheet" type="text/css" />');
            //  frameDoc.document.write('<link href="css/bootstrap.min.css" rel="stylesheet" type="text/css" />');
            frameDoc.document.write(contents);
            frameDoc.document.write('</body></html>');
            frameDoc.document.close();
            setTimeout(function () {
                window.frames["frame1"].focus();
                window.frames["frame1"].print();
                document.body.removeChild(frame1);
            }, 500);

            return false;
        }
    </script>
</head>
<body>
<div id="dvContents">

<h2>
Print div content using jquery
</h2>
<p>
C# Soft Code
</p>

</div>
<input type="button" id="btnPrint" value="Print" onclick="PrintDiv();" class="btn btn-xs btn-success warning_4" /></div>
</body>

</html>

No comments:

Post a Comment