【问题标题】:Not able to see the print preview in Google Chrome what might be the reason无法在谷歌浏览器中看到打印预览可能是什么原因
【发布时间】:2014-06-16 07:41:04
【问题描述】:

当我尝试在“Google Chrome”中打印页面时,它没有显示任何内容,但内容显示在“Mozilla Firefox”中。可能是什么原因?

以下是我用来打印页面特定部分的“javascript”代码 -

 function printItemsLevel(tableClass){
    var html="<html>";
    html+='<link rel="Stylesheet" type="text/css" href="{{ asset('css/bootstrap.css') }}"></link>';
    html+='<link rel="Stylesheet" type="text/css" href="{{ asset('css/bootstrap-theme.css') }}"></link>';
    html+= document.getElementsByClassName(tableClass)[0].innerHTML;
    html+="</html>";
    var printWin = window.open('','','left=0,top=0,width=960,height=960,toolbar=0,scrollbars=0,status =0');
    printWin.document.write(html);
    printWin.document.close();
    printWin.focus();
    printWin.print();
    printWin.close();
 }

先谢谢了。

【问题讨论】:

  • 我已经更新了我的答案.. 让我知道是否可行

标签: javascript google-chrome printing


【解决方案1】:

我认为问题应该是window.print之后的window.close。 Chrome 不等待打印完成

试试

 setTimeout(function(){ printWin.close();}, 10000); 

而不是

printWin.close();

这样你就可以通过这种方式检查浏览器是否是chrome

var is_chrome = navigator.userAgent.toLowerCase().indexOf('chrome') > -1;

如果为 true,则添加 setTimeout 函数,否则您的代码应该可以工作

总结

function printItemsLevel(tableClass){
    var is_chrome = navigator.userAgent.toLowerCase().indexOf('chrome') > -1;
    var html="<html>";
    html+='<link rel="Stylesheet" type="text/css" href="{{ asset('css/bootstrap.css') }}"></link>';
    html+='<link rel="Stylesheet" type="text/css" href="{{ asset('css/bootstrap-theme.css') }}"></link>';
    html+= document.getElementsByClassName(tableClass)[0].innerHTML;
    html+="</html>";
    var printWin = window.open('','','left=0,top=0,width=960,height=960,toolbar=0,scrollbars=0,status =0');
    printWin.document.write(html);
    printWin.document.close();
    printWin.focus();
    printWin.print();
    if(is_chrome){
      setTimeout(function(){ printWin.close();}, 10000); 
    }
    else{
     printWin.close();
    }

 }

如果它不起作用,我认为它可能是 innerHTML 中的错误

如果您插入像“&lt;header&gt;Test&lt;/header&gt;”这样的 html 代码,它可能无法在所有浏览器中正常工作。尝试插入纯文本并让我知道是否可行。如果是,我建议你以这种方式使用 jquery html 函数

$('.tableClass').html(data);

【讨论】:

  • 即使在修改了上面的代码后也面临同样的问题。还有什么我需要更改代码或任何可以使用 chrome 浏览器锻炼的代码的地方
  • 删除 printWin.document.close(); 或将其移出
  • 现在工作了吗?如果没有尝试将 onload="window.print()"onfocus="window.close()" 添加到打印窗口的 html 正文中
  • 您是否在单击锚标记时调用此函数?如果是,则在函数末尾添加return false
  • 我在单击带有触发器“onclick”的按钮时调用此函数。这是代码“ "
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-21
  • 1970-01-01
  • 1970-01-01
  • 2015-07-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多