Print HTML DIV contents in new tab window using JavaScript in ASP.Net

anjali600
 
on Sep 04, 2021 02:11 PM
731 Views

I am trying to print the contents of whatever is inside the div. I did that successfully, but the contents that needs to be printed comes in a popup window.

Is it possible to see the conetents that need to be printed in a new tab. Below is the javascript code to do the printing:

any help will be appreciated.

<script type="text/javascript" >
    function printdiv(printpage) {
        var headstr = "<html><head><title></title></head><body>";
        var footstr = "</body>";
        document.getElementById('<%=lblDateTime.ClientID %>').style.display = "block";
        document.getElementById('<%=lblDateTime.ClientID %>').innerHTML = new Date().toLocaleString();
        var newstr = document.all.item(printpage).innerHTML;
        var oldstr = document.body.innerHTML;
        document.body.innerHTML = headstr + newstr + footstr;
        w = window.open("", "_blank", "k").focus();
        w.document.write(headstr + newstr + footstr);
        w.print();
        document.body.innerHTML = oldstr;
        return false;
    }
</script>
<div id="div_print">
    this contenet needs to be printed in a new tab rather than a pop up window
</div>

any help will be highly apprecaited

Download FREE API for Word, Excel and PDF in ASP.Net: Download
dharmendr
 
on Sep 06, 2021 05:19 AM

Hi anjali600,

Refer below code.

HTML

<script type="text/javascript">
    function printdiv(printpage) {
        var headstr = "<html><head><title></title></head><body>";
        var footstr = "</body>";
        document.getElementById('<%=lblDateTime.ClientID %>').style.display = "block";
        document.getElementById('<%=lblDateTime.ClientID %>').innerHTML = new Date().toLocaleString();
        var newstr = document.all.item(printpage).innerHTML;
        var oldstr = document.body.innerHTML;
        var winPrint = window.open('', '_blank', 'left=0,top=0,width=800,height=600,toolbar=0,scrollbars=0,status=0');
        winPrint.document.write(headstr + newstr + footstr);
        winPrint.document.close();
        winPrint.focus();
        winPrint.print();
        winPrint.close();
        return false;
    }
</script>
<div id="div_print">
    this contenet needs to be printed in a new tab rather than a pop up window
</div>
<asp:Label ID="lblDateTime" runat="server" />
<asp:Button Text="text" runat="server" OnClientClick="printdiv('div_print')" />