.net 如何使用javascript从弹出窗口打印?
我有一个.Net应用程序,它可以动态创建一个小HTML页面,并使用javascript document.open方法在新窗口中弹出它。具有该功能的一切都工作正常 现在我想在打印页面的HTML页面中添加一个按钮。我已尝试使用以下代码,但无效:.net 如何使用javascript从弹出窗口打印?,.net,javascript,printing,popup,.net,Javascript,Printing,Popup,我有一个.Net应用程序,它可以动态创建一个小HTML页面,并使用javascript document.open方法在新窗口中弹出它。具有该功能的一切都工作正常 现在我想在打印页面的HTML页面中添加一个按钮。我已尝试使用以下代码,但无效: <a href='print.html' onClick='window.print();return false;'> <img src='images/printer.png' height='32px' width='32px'&g
<a href='print.html' onClick='window.print();return false;'>
<img src='images/printer.png' height='32px' width='32px'></a>
在弹出窗口中单击按钮时,不会发生任何事情。但是,当此页面的源代码作为单独的页面保存并加载到浏览器中时,“打印”按钮工作正常。因此,问题似乎是由代码位于弹出窗口中这一事实引起的。[现在的问题似乎是,在弹出窗口打开后,代码会写入其中。]有人知道解决此问题的方法或其他方法吗
编辑:
我尝试过的其他方法具有相同的结果:
<input type='button' onclick='window.print()' value='Print' />
及
再次编辑:
上述代码在Firefox中有效,但在IE7中无效。有没有关于IE解决方案的想法
再次编辑:
下面是一个使用下面代码的测试用例。我打开一个空白url,然后将代码写入其中,而不是单独的html文件中的弹出窗口代码。此步骤似乎是导致问题的原因
<html>
<head>
<title>main</title>
</head>
<body>
<h1>
Pop & print</h1>
<button onclick="pop();">
Pop</button>
<script type="text/javascript">
var POP;
function pop() {
var newWin = window.open('', 'thePopup', 'width=350,height=350');
newWin.document.write("<html><head><title>popup</title></head><body><h1>Pop</h1>" +
"<p>Print me</p><a href='print.html' onclick='window.print();return false;'>" +
"<img src='images/printer.png' height='32px' width='32px'></a></body></html>");
}
</script>
</body>
</html>
主要的
流行与印刷
流行音乐
var-POP;
函数pop(){
var newWin=window.open(“”,'thepoup','width=350,height=350');
newWin.document.write(“popupPop”+
“打印我””;
}
这可能是因为在锚标记的onclick事件中执行了返回false
试试这个:
<input type="button" onclick="window.print()" value="Print" />
您可以尝试:
<input type="button" onclick="self.print()" value="Print" />
或:
但由于中的限制,这在MSIE中可能不起作用。我认为,最好的方法是把打印按钮放在主窗口上
<script language="javascript">
var winref = window.open('print.html','windowName','width=400,height=400');
</script>
<form>
<input type="button" value="Print Popup" onclick="if (window.print) winref.print()">
</form>
var winref=window.open('print.html','windowName','width=400,height=400');
如果您想打印从中打开的窗口中的内容,我建议您使用
window.opener.print();
在弹出窗口中。必须有比显示的代码更多的内容。我认为它工作得很好(正在测试一些) 这是一个很小的测试用例。在你的设置中尝试一下,看看会发生什么!我的检查是在WindowsVista、IE7和IE8下进行的 main.html:
<html>
<head>
<title>main</title>
</head>
<body>
<h1>Pop & print</h1>
<button onclick="pop();">Pop</button>
<script type="text/javascript">
var POP;
function pop() {
POP = window.open('popup.html', 'thePopup', 'width=350,height=350');
}
</script>
</body>
</html>
主要的
流行与印刷
流行音乐
var-POP;
函数pop(){
POP=window.open('popup.html','thePopup','width=350,height=350');
}
popup.html:
<html>
<head>
<title>popup</title>
</head>
<body>
<h1>Pop</h1>
<p>Print me</p>
<a href="print.html" onclick="window.print();return false;">
<img src="images/printer.png" height="32px" width="32px">
</a>
</body>
</html>
弹出窗口
流行音乐
打印我
我用标准HTML标记创建了一个空白HTML页面,解决了这个问题。然后,我通过创建一个新的DOM元素并编辑innerHTML来添加内容。生成的代码与示例中的代码相同,只是将newWin.document.write命令替换为以下命令:
var newDiv = newWin.document.createElement( 'div' );
newDiv.innerHTML = "<h1>Pop</h1>" +
"<p>Print me</p><a href='print.html' onclick='window.print();return false;'>" +
"<img src='images/printer.png' height='32px' width='32px'></a>"
newWin.document.body.appendChild(newDiv);
var newDiv=newWin.document.createElement('div');
newDiv.innerHTML=“Pop”+
“打印我”
newWin.document.body.appendChild(newDiv);
虽然问题已经解决,但老实说,我不确定问题的确切原因是什么。如果有人有什么想法,我很高兴听到。你忘了:
newWin.document.close();
必须先关闭文档,msie才能打印它。以下是一个对我有效的解决方案:
newWin.document.write( newhtml );
newWin.window.location.reload(); // this is the secret ingredient
newWin.focus(); // not sure if this line is necessary
newWin.print();
我不能100%确定为什么会这样,但我认为这与以下问题之一有关:(1)IE安全问题,(2)范围问题(即创建新文档后,IE对打印哪个文档感到困惑),或(3)时间问题-文档尚未准备好“接受”打印命令。在任何情况下,重新加载后,打印对话框都不会出现问题 我想创建一个对打印机友好的页面版本,然后自动打印,这就是我想到的,似乎对我很有用
function printPage(){
var w = window.open();
var headers = $("#headers").html();
var field= $("#field1").html();
var field2= $("#field2").html();
var html = "<!DOCTYPE HTML>";
html += '<html lang="en-us">';
html += '<head><style></style></head>';
html += "<body>";
//check to see if they are null so "undefined" doesnt print on the page. <br>s optional, just to give space
//This allows you to reuse this on lots of pages with the same template
if(headers != null) html += headers + "<br/><br/>";
if(field != null) html += field + "<br/><br/>";
if(field2 != null) html += field2 + "<br/><br/>";
html += "</body>";
w.document.write(html);
w.window.print();
w.document.close();
};
函数printPage(){
var w=window.open();
var headers=$(“#headers”).html();
var field=$(“#field1”).html();
var field2=$(“#field2”).html();
var html=“”;
html+='';
html+='';
html+=“”;
//检查它们是否为空,以便“undefined”不会打印在页面上。
s可选,只是为了留出空间
//这允许您在具有相同模板的许多页面上重复使用该模板
如果(headers!=null)html+=headers+“
”;
如果(field!=null)html+=field+“
”;
如果(field2!=null)html+=field2+“
”;
html+=“”;
w、 document.write(html);
w、 window.print();
w、 document.close();
};
这在Chrome中起作用:
<body ><img src="image.jpg" alt="" style="display: block; width: 100%; height: 100%;">
<script type="text/javascript">
window.onload = function() {
window.print();
setTimeout(function() {
window.close();
}, 1);
};
</script>
</body>
window.onload=函数(){
window.print();
setTimeout(函数(){
window.close();
}, 1);
};
这有相同的结果,在单独的浏览器窗口中工作,但不是从弹出窗口。嗯!当我点击按钮时,它会打开打印机堆栈供我选择打印机。你们有打印机吗?也许它找不到打印机?但无论如何,它应该打开打印机选择框。您是否尝试从弹出窗口中输入代码?这似乎是导致问题的原因。是的,我的打印按钮在弹出窗口内!当我单击“打印”按钮时,它会打开选择打印机的对话框。您使用的浏览器是什么?我刚刚在Firefox中测试了这个网站,它成功了。但在IE7中仍然没有。我希望打印弹出窗口的内容,而不是主窗口。前两种方法都不起作用。第三种方法并不理想,而且变得更加复杂,因为HTML页面的代码是根据各种参数从.Net代码后面生成的。很抱歉,这是个坏消息。如果是我,在生成打开弹出窗口的按钮/链接的代码中,我会创建两个。“显示结果”a
function printPage(){
var w = window.open();
var headers = $("#headers").html();
var field= $("#field1").html();
var field2= $("#field2").html();
var html = "<!DOCTYPE HTML>";
html += '<html lang="en-us">';
html += '<head><style></style></head>';
html += "<body>";
//check to see if they are null so "undefined" doesnt print on the page. <br>s optional, just to give space
//This allows you to reuse this on lots of pages with the same template
if(headers != null) html += headers + "<br/><br/>";
if(field != null) html += field + "<br/><br/>";
if(field2 != null) html += field2 + "<br/><br/>";
html += "</body>";
w.document.write(html);
w.window.print();
w.document.close();
};
<body ><img src="image.jpg" alt="" style="display: block; width: 100%; height: 100%;">
<script type="text/javascript">
window.onload = function() {
window.print();
setTimeout(function() {
window.close();
}, 1);
};
</script>
</body>