Javascript 如何使用document.write函数在printview中动态加载图像
我正在尝试使用Javascript 如何使用document.write函数在printview中动态加载图像,javascript,servicenow,Javascript,Servicenow,我正在尝试使用document.write函数动态加载图像。请在下面查找代码。图像有时被加载,有时不显示 var mywindow = window.open('', 'PRINT', 'height=400,width=600'); mywindow.document.write('<html><head><title>'+"Contract Approval Form"+'</title>'); mywindow.do
document.write
函数动态加载图像。请在下面查找代码。图像有时被加载,有时不显示
var mywindow = window.open('', 'PRINT', 'height=400,width=600');
mywindow.document.write('<html><head><title>'+"Contract Approval Form"+'</title>');
mywindow.document.write('</head><body >');
mywindow.document.write('<div id="headerLogo"></div>');
mywindow.document.write('<div id="watermarkDiv"></div>');
mywindow.document.write('<div style="margin-top:50px;" id="content">' +printContent+ '</div>');
var img = mywindow.document.createElement('img');
img.setAttribute('src', 'mylogo.png');
mywindow.document.getElementById("headerLogo").appendChild(img);
mywindow.document.write('</body></html>');
mywindow.document.close(); // necessary for IE >= 10
mywindow.focus(); // necessary for IE >= 10*/
mywindow.print();
mywindow.close();
var mywindow=window.open(''PRINT','height=400,width=600');
mywindow.document.write(“+”合同审批表“+”);
mywindow.document.write(“”);
mywindow.document.write(“”);
mywindow.document.write(“”);
mywindow.document.write(“”+printContent+“”);
var img=mywindow.document.createElement('img');
setAttribute('src','mylogo.png');
mywindow.document.getElementById(“headerLogo”).appendChild(img);
mywindow.document.write(“”);
mywindow.document.close();//IE>=10所必需的
mywindow.focus();//IE>=10所必需的*/
mywindow.print();
mywindow.close();
从中找到解决方案:
似乎我需要使用setTimeout
setTimeout(function() {
newWindow.print();
newWindow.close();
}, 250);
从找到解决方案: 似乎我需要使用setTimeout
setTimeout(function() {
newWindow.print();
newWindow.close();
}, 250);
它是关于等待内容加载的
<script>
var printContent = '<h1>Hello World</h1>';
var mywindow = window.open('about:blank', 'PRINT', 'height=400,width=600');
//~ mywindow.onload = function() {
mywindow.document.write('<html><head><title>'+"Contract Approval Form"+'</title>');
mywindow.document.write('</head><body >');
mywindow.document.write('<div id="headerLogo"></div>');
mywindow.document.write('<div id="watermarkDiv"></div>');
mywindow.document.write('<div style="margin-top:50px;" id="content">' +printContent+ '</div>');
var img = mywindow.document.createElement('img');
img.setAttribute('src', 'mylogo.png');
img.onload = function() {
// now you can print, need to wait for image to load - which connotes you need to work out how to wait for all content to load
mywindow.focus();
mywindow.print();
}
mywindow.document.getElementById("headerLogo").appendChild(img);
mywindow.document.write('</body></html>');
</script>
var printContent='Hello World';
var mywindow=window.open('about:blank','PRINT','height=400,width=600');
//~mywindow.onload=函数(){
mywindow.document.write(“+”合同审批表“+”);
mywindow.document.write(“”);
mywindow.document.write(“”);
mywindow.document.write(“”);
mywindow.document.write(“”+printContent+“”);
var img=mywindow.document.createElement('img');
setAttribute('src','mylogo.png');
img.onload=函数(){
//现在您可以打印,需要等待图像加载-这意味着您需要了解如何等待所有内容加载
mywindow.focus();
mywindow.print();
}
mywindow.document.getElementById(“headerLogo”).appendChild(img);
mywindow.document.write(“”);
等待内容加载
<script>
var printContent = '<h1>Hello World</h1>';
var mywindow = window.open('about:blank', 'PRINT', 'height=400,width=600');
//~ mywindow.onload = function() {
mywindow.document.write('<html><head><title>'+"Contract Approval Form"+'</title>');
mywindow.document.write('</head><body >');
mywindow.document.write('<div id="headerLogo"></div>');
mywindow.document.write('<div id="watermarkDiv"></div>');
mywindow.document.write('<div style="margin-top:50px;" id="content">' +printContent+ '</div>');
var img = mywindow.document.createElement('img');
img.setAttribute('src', 'mylogo.png');
img.onload = function() {
// now you can print, need to wait for image to load - which connotes you need to work out how to wait for all content to load
mywindow.focus();
mywindow.print();
}
mywindow.document.getElementById("headerLogo").appendChild(img);
mywindow.document.write('</body></html>');
</script>
var printContent='Hello World';
var mywindow=window.open('about:blank','PRINT','height=400,width=600');
//~mywindow.onload=函数(){
mywindow.document.write(“+”合同审批表“+”);
mywindow.document.write(“”);
mywindow.document.write(“”);
mywindow.document.write(“”);
mywindow.document.write(“”+printContent+“”);
var img=mywindow.document.createElement('img');
setAttribute('src','mylogo.png');
img.onload=函数(){
//现在您可以打印,需要等待图像加载-这意味着您需要了解如何等待所有内容加载
mywindow.focus();
mywindow.print();
}
mywindow.document.getElementById(“headerLogo”).appendChild(img);
mywindow.document.write(“”);
setTimeout也可以,但onload是更好的解决方案。setTimeout也可以,但onload是更好的解决方案。