[英]why window.open() overwhenlming while opening the same file?
m trying to print a web page using javascript. 我试图使用javascript打印网页。 But window.open() is not loading the style sheets of the same file :(
但是window.open()不会加载同一文件的样式表:(
here is my code of javascript 这是我的javascript代码
function Print() {
printWindow = window.open("", "mywindow", "location=1,status=1,scrollbars=1,width=600,height=600");
printWindow.document.write("<div style='width:100%;'>");
printWindow.document.write("<input type='button' id='btnPrint' value='Print' style='width:100px' onclick='window.print()' />");
printWindow.document.write("<input type='button' id='btnCancel' value='Cancel' style='width:100px' onclick='window.close()' />");
printWindow.document.write("<select id='cboprintSize' onchange='document.getElementById(\"divContainer\").style.width = document.getElementById(\"cboprintSize\").value + \"px\";'><option value=\"300\">A4</option><option value=\"500\">A5</option></select>");
printWindow.document.write("<div id='divContainer' style='width:500; height:700; background-color:white'>");
printWindow.document.write("<table width='100%'><tr><td>");
printWindow.document.write(document.getElementById('printForm').innerHTML);
printWindow.document.write("</td></tr><table>");
printWindow.document.write("</div>");
printWindow.document.write("</div>");
printWindow.document.close();
printWindow.focus();
}
and here is the asp.net form 这是asp.net表格
<input type="button" id="btnCall" onclick="Print()" value="Print" />
<form runat="server" id="printForm">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td colspan="3">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td>
<img src="images/incident1.png" alt="First Line Incident" title="First Line Incident" align="absmiddle" class="cardIcon" border="0">
</td><td width="100%"><span class="title"> PRI1307-006</span></td>
<td align="right" nowrap>
<a href="#" id="public_edit">
<span class="value">
<img src="images/edit.png" class="iconNavBar" style="border: 0px;" title="Edit" width="24" height="24"></span>
</a>
<a href="#" dynamicMenu="print" target="_blank">
<span class="value">
<img src="images/icon_printable_version.png" class="iconNavBar" style="border: 0px;" title="Printable version" width="24" height="24">
</span></a></td>
</tr>
</table>
</form>
You are creating a document in a new window. 您正在新窗口中创建文档。 The stylesheet(s) of the parent page cannot load because it is a new page in a new window.
无法加载父页面的样式表,因为它是新窗口中的新页面。
What you can do is while creating the document in the new window, create the complete html linking your external stylesheet: 您可以做的是在新窗口中创建文档时,创建链接外部样式表的完整html:
printWindow.document.write("<html><head>");
printWindow.document.write("<link href='style.css' type='text/css' rel='stylesheet' />");
printWindow.document.write("</head><body>");
...
printWindow.document.write("<div style='width:100%;'>");
...
printWindow.document.write("</body></html>");
printWindow.document.close();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.