Window.print()不工作,但在所有其他浏览器中工作

时间:2014-01-27 12:39:47

标签: javascript

我知道我要问的问题已经在这里讨论了。但我找不到任何明确的解决方案。 我正在Drupal 7开展一个项目,我想打印出来。我有图表和其他一些内容要打印。我使用window.open()创建了新窗口,并将所有样式和js(chart.jsjquery.js等)附加到新创建的窗口以获取样式。并打印window.print()。我的代码在firefoxchrome中工作,但不在IE中。我附上了我在下面使用的代码。

var mywindow = window.open('', 'print', 'scrollbars=1,height=600,width=600');
mywindow.document.write("<html><head><title>Patient Data</title><link rel='stylesheet' type='text/css' href='" + Drupal.settings.basePath + "sites/all/themes/prescribewell/css/style.css'>");
mywindow.document.write("<link rel='stylesheet' type='text/css' href='" + Drupal.settings.basePath + "sites/all/themes/prescribewell/css/bootstrap.css'>");
mywindow.document.write("<link rel='stylesheet' type='text/css' href='" + Drupal.settings.basePath + "sites/all/themes/prescribewell/css/bootstrap.min.css'>");
mywindow.document.write("<link rel='stylesheet' type='text/css' href='" + Drupal.settings.basePath + "sites/all/themes/prescribewell/css/bootstrap-theme.css'>");
mywindow.document.write("<link rel='stylesheet' type='text/css' href='" + Drupal.settings.basePath + "sites/all/themes/prescribewell/css/bootstrap-theme.min.css'>");
mywindow.document.write("<link rel='stylesheet' type='text/css' href='" + Drupal.settings.basePath + "sites/all/themes/prescribewell/css/font-awesome.css'>");
mywindow.document.write("<link rel='stylesheet' type='text/css' href='" + Drupal.settings.basePath + "sites/all/themes/prescribewell/css/font-style.css'>");
mywindow.document.write("<script src='http://code.jquery.com/jquery-1.10.2.min.js'></sc" + "ript>");
mywindow.document.write("<script src='" + Drupal.settings.basePath + "sites/all/themes/prescribewell/js/bootstrap.js'></sc" + "ript>");
mywindow.document.write("<script src='" + Drupal.settings.basePath + "sites/all/themes/prescribewell/js/bootstrap.min.js'></sc" + "ript>");
mywindow.document.write("<script src='" + Drupal.settings.basePath + "sites/all/themes/prescribewell/js/button.js'></sc" + "ript>");
mywindow.document.write("<script src='" + Drupal.settings.basePath + "sites/all/themes/prescribewell/js/prescribewell_leads_js.js'></sc" + "ript>");
mywindow.document.write("<script src='" + Drupal.settings.basePath + "sites/all/modules/Prescribewell_leeds/prescription.js'></sc" + "ript>");

mywindow.document.write("</head><body>");
mywindow.document.write(datas.resPres); //content to be printed as json response...

mywindow.document.write("</body>");
if (summary == 1) {
    mywindow.document.write("<script src='http://code.jquery.com/jquery-1.10.2.min.js'></sc" + "ript>");
    mywindow.document.write("<script src='" + Drupal.settings.basePath + "sites/all/modules/prescribewell_charts/Chart.js'></sc" + "ript>");
    mywindow.document.write("<script src='" + Drupal.settings.basePath + "sites/all/modules/prescribewell_charts/clinicchart.js'></sc" + "ript>");
    mywindow.document.write("<script src='" + Drupal.settings.basePath + "sites/all/modules/Prescribewell_leeds/print.js'></sc" + "ript>");
}
mywindow.document.write("</html>");

if (navigator.appName == 'Microsoft Internet Explorer') {
    window.print();
} else {

    setTimeout(function () {

        mywindow.document.close();
        mywindow.focus();
        mywindow.print();
        //mywindow.close();

    }, 5000);
}

提前致谢。

1 个答案:

答案 0 :(得分:1)

您的代码具有IE的条件:

if (navigator.appName == 'Microsoft Internet Explorer') {
    window.print();
} else {
    //other code
}

其他代码块是跨浏览器,必须在IE中运行:

mywindow.document.close();//close document after write
mywindow.focus();//set focus back
mywindow.print();//start print dialog
//mywindow.close();

因此,只需if/else检查即可删除IE