未使用Javascript将图像复制到新的html页面

未使用Javascript将图像复制到新的html页面,javascript,printing,Javascript,Printing,我有一个按钮,可以将html页面的内容复制到新页面,然后调用浏览器的打印功能 唯一的问题是它没有跨 JS: 函数PrintElem(elem) { 弹出($(elem.html()); } 功能弹出窗口(数据) { var mywindow=window.open(“”,'mydiv','height=600,width=800'); mywindow.document.write('Hire Form'); mywindow.document.write(“”); mywindow.docum

我有一个按钮,可以将html页面的内容复制到新页面,然后调用浏览器的打印功能

唯一的问题是它没有跨

JS:

函数PrintElem(elem)
{
弹出($(elem.html());
}
功能弹出窗口(数据)
{
var mywindow=window.open(“”,'mydiv','height=600,width=800');
mywindow.document.write('Hire Form');
mywindow.document.write(“”);
mywindow.document.write(数据);
mywindow.document.write(“”);
mywindow.document.close();//对于IE>=10是必需的
mywindow.focus();//IE>=10所必需的
mywindow.print();
mywindow.close();
返回true;
}
html Img:

<img src="../../../Corp/SiteAssets/Intranet%20Branding/Logo.png" alt="Test"></img>

编辑 当我将img url更改为其完整url时,它在IE和Firefox中工作,但在chrome中不工作。

我可以理解你的观点

请看

函数PrintElem(elem){
弹出(“asdfsdf”);
}
功能弹出窗口(数据){
var mywindow=window.open(“”,'mydiv','height=600,width=800');
mywindow.document.write('Hire Form');
mywindow.document.write(“”);
mywindow.document.write(数据);
mywindow.document.write(“”);
mywindow.document.close();//对于IE>=10是必需的
mywindow.focus();//IE>=10所必需的
//听img onload然后调用print
mywindow.document.getElementsByTagName('img')[0]。onload=function(){
mywindow.print();
mywindow.close();
};
返回true;
}
PrintElem();

您可以将onload事件添加到主体中,这样就不需要修改或添加每个图像元素的属性,如下所示

function Popup(data) 
{
    var mywindow = window.open('', 'my div', 'height=600,width=800');
    mywindow.document.write('<html><head><title>Hire Form</title>');

    mywindow.document.write('</head><body onload="window.print(); window.close();">');
    mywindow.document.write(data);
    mywindow.document.write('</body></html>');

    mywindow.document.close(); // necessary for IE >= 10
    mywindow.focus(); // necessary for IE >= 10

    return true; 
}
功能弹出窗口(数据)
{
var mywindow=window.open(“”,'mydiv','height=600,width=800');
mywindow.document.write('Hire Form');
mywindow.document.write(“”);
mywindow.document.write(数据);
mywindow.document.write(“”);
mywindow.document.close();//对于IE>=10是必需的
mywindow.focus();//IE>=10所必需的
返回true;
}

?可能图像尚未加载,您需要调用
mywindow.print()图像完全加载后。@AdrianDelaPiedra您如何判断图像何时加载?@ArunPJohny您的小提琴工作正常,这就是我希望我的代码工作的方式。如果它能带来不同(它不应该…),那么环境就是两件事。您在PrintElem()中输入了什么?一个潜在的问题是您正在使用window.close()立即关闭弹出窗口;如果你正在使用一个本地文件:/,用于可能导致错误的图像,或者你可以使用@Adrian Dela Piedra说的,它做了sameSee OP edit。在chrome中不工作,通过此检查,chrome中永远不会出现打印窗口(加载或按按钮)。
function PrintElem(elem) {
  Popup('asdfsdf <img id="needImage" src="//placehold.it/64/fff000" alt="Test"></img>');
}

function Popup(data) {
  var mywindow = window.open('', 'my div', 'height=600,width=800');
  mywindow.document.write('<html><head><title>Hire Form</title>');

  mywindow.document.write('</head><body >');
  mywindow.document.write(data);
  mywindow.document.write('</body></html>');

  mywindow.document.close(); // necessary for IE >= 10
  mywindow.focus(); // necessary for IE >= 10

  // listen img onload then call print
  mywindow.document.getElementsByTagName('img')[0].onload = function(){
    mywindow.print();
    mywindow.close();
  };

  return true;
}

PrintElem();
function Popup(data) 
{
    var mywindow = window.open('', 'my div', 'height=600,width=800');
    mywindow.document.write('<html><head><title>Hire Form</title>');

    mywindow.document.write('</head><body onload="window.print(); window.close();">');
    mywindow.document.write(data);
    mywindow.document.write('</body></html>');

    mywindow.document.close(); // necessary for IE >= 10
    mywindow.focus(); // necessary for IE >= 10

    return true; 
}