如果用户单击缩略图,我有一个旧的javascript代码可以打印图像。过去工作正常,但最近(仅在Chrome中!)预览中有空白页。
这是JsBin中的演示:http ://jsbin.com/yehefuwaso/7单击打印机图标。现在在Firefox中尝试;它会按预期工作。
铬:41.0.2272.89 m
Firefox:30.0、36.0.1
function newWindow(src){
win = window.open("","","width=600,height=600");
var doc = win.document;
// init head
var head = doc.getElementsByTagName("head")[0];
// create title
var title = doc.createElement("title");
title.text = "Child Window";
head.appendChild(title);
// create script
var code = "function printFunction() { window.focus(); window.print(); }";
var script = doc.createElement("script");
script.text = code;
script.type = "text/javascript";
head.appendChild(script);
// init body
var body = doc.body;
//image
doc.write('<img src="'+src+'" width="300">');
//chrome
if (navigator.userAgent.toLowerCase().indexOf('chrome') > -1) {
win.printFunction();
} else {
win.document.close();
win.focus();
win.print();
win.close();
}
}
看起来它正在尝试在<img>
加载之前进行打印,例如通过将链接打开为数据URI或Blob来将调用移动到事件处理程序中以进行load事件,例如window
var code = '\
<html>\
<head>\
<title></title>\
<script>\
function printFunction() {\
window.focus();\
window.print();\
window.close();\
}\
window.addEventListener(\'load\', printFunction);\
</script>\
</head>\
<body><img src="'+src+'" width="300"></body>\
</html>';
window.open('data:text/html,' + code, '_blank', 'width=600,height=600');
别忘了,您可能需要对标签进行HTML编码code
你很可能只是监听负荷的<img>
替代,但如果你做任何事情比特林打印您可能会发现一个单一的形象更加复杂,未来再次爆发
doc.write('<img onload="printFunction();" src="'+src+'" width="300">');
printFunction
所有浏览器的打印功能在哪里
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句