Javascript:Window.print()之后的空html div内容
单击按钮时,我正在使用javascript的Javascript:Window.print()之后的空html div内容,javascript,google-chrome,printing,Javascript,Google Chrome,Printing,单击按钮时,我正在使用javascript的window.print()来打印print\u div内容 但是由于一些安全问题,我想避免多重打印(比如使用Cntrl+p)选项,所以我想清除print\u div内容,用户不能一次又一次地重新打印 这里是粗略的代码 document.getElementById("print_div").innerHTML = //Some contents to be printed on paper. window.print() document.getEl
window.print()
来打印print\u div
内容
但是由于一些安全问题,我想避免多重打印(比如使用Cntrl+p)选项,所以我想清除print\u div
内容,用户不能一次又一次地重新打印
这里是粗略的代码
document.getElementById("print_div").innerHTML = //Some contents to be printed on paper.
window.print()
document.getElementById("print_div").innerHTML = '' // Clearing old contents to avoid mis-use
但这段代码根本不起作用,它在创建打印预览之前清除了print\u div
内容,就像在chrome中一样。(我猜,是异步工作的)
谁能告诉我,我哪里做错了
注意:我使用
Chrome:22.0.1229.92 m
来测试我的代码&我只想使用Chrome。您可以使用setTimeout方法并尝试
setTimeout("your code or function etc",mili seconds)
setTimeout("document.getElementById('print_div').innerHTML = ''",5000);
您正在print_div中打印,在下一步中,您将清除相同的内容。因此它显示为空。可以将打印数据存储在变量中。然后将其显示在print_div中,您可以清除该变量
var print = "";
print += "<p>Something </p><br/>";
print += "<table><tr><td>Something</td><td>Something</td></tr>
<tr><td>Something</td><td>Something</td></tr> </table>";
document.getElementById("print_div").innerHTML = print;
print = "";
var print=”“;
打印+=“某物”
”;
打印+=“某物某物”
“某物某物”;
document.getElementById(“print\u div”).innerHTML=print;
print=“”;
像这样的。将此javascript设置为button onsubmit事件。我不赞成设置超时,它只有在用户立即确认打印时才起作用,这是不能盲目假设的 我会做如下的事情:
var html = "<html><head></head><body>" + $("print_div").html() + "</body></html>";
var newWindow= window.open("", "PrintWindow",
"width=100,height=50,top=50,left=50,toolbars=no,scrollbars=yes,status=no,resizable=yes");
newWindow.document.writeln(html);
newWindow.document.close();
newWindow.focus();
newWindow.print();
newWindow.close();
这是因为打印窗口未加载
var showPrintWindow = function (context) {
var printWindow = window.open('', '');
var doc = printWindow.document;
doc.write("<html><head>");
doc.write("<link href='/css/printReport.css' rel='stylesheet' type='text/css' media='print' />");
doc.write("</head><body>");
doc.write(context);
doc.write("</body></html>");
doc.close();
function show() {
if (doc.readyState === "complete") {
printWindow.focus();
printWindow.print();
printWindow.close();
} else {
setTimeout(show, 100);
}
};
show();
};
var showPrintWindow=函数(上下文){
var printWindow=window.open(“”,”);
var doc=printWindow.document;
文件以(“”)号填写;
文件以(“”)号填写;
文件以(“”)号填写;
文件编写(上下文);
文件以(“”)号填写;
doc.close();
函数show(){
如果(doc.readyState==“完成”){
printWindow.focus();
printWindow.print();
printWindow.close();
}否则{
设置超时(显示,100);
}
};
show();
};
它会工作的在Chrome中遇到了与您类似的问题,它在打印预览中显示空白页。尝试使用setTimeout,但无效。起作用的是window.onload
<div id="printableArea">
<h1>Print me</h1>
</div>
<input type="button" onclick="printDiv('printableArea')" value="print a div!" />
打印我
javascript:
function printDiv(divName) {
var printContents = document.getElementById(divName).innerHTML;
w = window.open();
w.document.write(printContents);
w.document.write('<scr' + 'ipt type="text/javascript">' + 'window.onload = function() { window.print(); window.close(); };' + '</sc' + 'ript>');
w.document.close(); // necessary for IE >= 10
w.focus(); // necessary for IE >= 10
return true;
}
函数printDiv(divName){
var printContents=document.getElementById(divName).innerHTML;
w=window.open();
w、 文件。书写(打印内容);
w、 document.write(“+”window.onload=function(){window.print();window.close();};“+”);
w、 document.close();//对于IE>=10是必需的
w、 focus();//对于IE>=10是必需的
返回true;
}
是的……我现在就是这么做的……非常感谢……)SetTimeout函数在其clear print div之后等待5秒。在您的代码中,print div是clear并不等待打印。如果我在这个页面上打开devtools并键入:document.getElementById(“content”).innerHTML=“Test”;window.print();document.getElementById(“内容”).innerHTML=''代码>一切都将按预期工作。我将使用“Test”获取预览,当我关闭打印对话框时,content
div将为空window.print()
似乎不是异步的。是的。它在devtools中工作正常,但在实际代码中却不工作。顺便说一句,我试图通过使用setTimeOut来清除这个问题,这很奇怪,但对我来说是有效的:)我认为你的解决方案没有多大意义。清除print
变量后document.getElementById(“print\u div”)
仍将包含仍可打印的数据。我们想阻止用户在此处多次打印。请使用更多信息进行编辑。不鼓励只编写代码和“试试这个”答案,因为它们不包含可搜索的内容,也不解释为什么有人应该“试试这个”。我们在这里努力成为知识的资源。为什么要像('
<div id="printableArea">
<h1>Print me</h1>
</div>
<input type="button" onclick="printDiv('printableArea')" value="print a div!" />
function printDiv(divName) {
var printContents = document.getElementById(divName).innerHTML;
w = window.open();
w.document.write(printContents);
w.document.write('<scr' + 'ipt type="text/javascript">' + 'window.onload = function() { window.print(); window.close(); };' + '</sc' + 'ript>');
w.document.close(); // necessary for IE >= 10
w.focus(); // necessary for IE >= 10
return true;
}