javascript - window.print() 在 Internet Explorer 中挂起

标签 javascript jquery internet-explorer internet-explorer-8

我正在尝试打开一个窗口并打印内容。

var parentHeader= $("head").html();
var parentDivContent = $("#printer_data_1").html();
var newW= window.open("", "", "scrollbars=1,width=200,height=200");
    newW.document.write('<html><head><title>Title</title>'+parentHeader+'</head>');
    newW.document.write('<body><div>');
    newW.document.write(parentDivContent );
    newW.document.write('</div></body></html>');
    newW.document.close();
    newW.print();
    newW.close();

当我尝试运行上面的 code 时,它在 IE8 中挂起,当我评论下面的行时......它有效。

newW.document.write('<html><head><title>Title</title>'+parentHeader+'</head>');

我希望在新窗口的头部部分加载全部内容和样式。

最佳答案

want the entire content, styles to be loaded in the head section of new window

尝试将 newWhtml 编译成单个字符串,包括 head html parentHeader ;在调用 window.open() 的第二个参数中为 newW 设置 name 引用;使用单个 html 字符串作为参数调用 document.write

var parentHeader= $("head").html();
var parentDivContent = $("#printer_data_1").html();
var newW= window.open("", "newW", "scrollbars=1,width=200,height=200");
var html = "<html>" 
           + parentHeader // `parentHeader` `head` `html`
           + "<body><div>" 
           + parentDivContent 
           + "</div></body></html>"
newW.document.write(html);
newW.document.close();
newW.print();
newW.close();

关于javascript - window.print() 在 Internet Explorer 中挂起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31215457/

相关文章:

javascript - 如何获取 knockoutjs 可观察数组的下一个元素?

php - 使用 jQuery 获取表单数组的值

javascript - IE8 将缓存中的动态 iframe 内容重新加载到错误的 iframe 中

javascript - 将字符串转换为数组,删除 + 字符

javascript - 如何从jquery下拉按钮控制检查元素

javascript - d3.在 inkscape :label ="foo" 上选择

javascript - 我的幻灯片只循环播放一张照片

javascript - 在ie9中上传得到js错误: SCRIPT5007: Object expected

vba - 点击链接后如何导航回来?

javascript - 设备架构中调用的遥测中的数组字段类型是什么?