I am trying to print a specific part of my application. My div. It works in html single page, but not in my application. My page is in ASP.NET and c#, but as I could not embed the function in C#, i am trying through javascript. However, there is a problem with the property inner.html
error:
0x800a138f - JavaScript runtime error: Unable to get property 'innerHTML' of undefined or null reference
code:
<script type="text/javascript">
function printdiv
var prtContent = document.getElementById("specific");
var WinPrint = window.open('', '', 'left=0,top=0,width=800,height=900,toolbar=0,scrollbars=0,status=0');
WinPrint.document.write(prtContent.innerHTML);
WinPrint.document.close();
WinPrint.focus();
WinPrint.print();
WinPrint.close();
</script>
<asp:Button ID="printButton" runat="server" Text="Print Results" OnClientClick="printdiv()"/>
My page is in asp, maybe that's the problem? How can i make it equivalent to print the div i want specifically?
edit, i don't know if it is relevant: specific part of div
Use the below code. Happy coding.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></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><title>DIV Contents</title>');
frameDoc.document.write('</head><body>');
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>
<form id="form1">
<span style="font-size: 10pt; font-weight: bold; font-family: Arial">Sample by Manoj Bhardwaj</span>
<hr />
<div id="dvContents" style="border: 1px dotted black; padding: 5px; width: 300px">
<span style="font-size: 10pt; font-weight: bold; font-family: Arial">Hi,
<br />
This is <span style="color: #18B5F0">a sample code my manoj</span>.<br />
i hope this will help you to print a div</span>
</div>
<br />
<input type="button" onclick="PrintDiv();" value="Print" />
</form>
</body>
</html>
i hope this will help you out.
You have to change OnClientClick from OnClientClick="printdiv()" to OnClientClick=" return printdiv()"
so your code suppose to
<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><title>DIV Contents</title>');
frameDoc.document.write('</head><body>');
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>
<form id="form1" runat="server">
<span style="font-size: 10pt; font-weight: bold; font-family: Arial">Sample by Dharmin Shah</span>
<hr />
<div id="dvContents" style="border: 1px dotted black; padding: 5px; width: 300px" runat="server">
<span style="font-size: 10pt; font-weight: bold; font-family: Arial">Hi,
<br />
This is <span style="color: #18B5F0">Dharmin's sample code</span>.<br />
i hope this will help you to print a div</span>
</div>
<br />
<asp:Button ID="Button" runat="server" Text="Button" OnClientClick="return PrintDiv();" value="Print" />
</form>
</body>
User contributions licensed under CC BY-SA 3.0