Balu
Balu

Reputation: 607

Window.print() is not working in ie, but working in all other browsers

I know the question I am asking has been already discussed here. But I couldnt find any definite solution. I am working on a project in Drupal 7, where I want to take print out. I have charts and some other contents to be printed. I used window.open() to created new window and append all the styles and js (chart.js, jquery.js etc) to newly created window to get the style. And window.print() to print. My code is working in firefox and chrome, but not in IE. I am attaching the code I used below.

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);
}

Thanks in advance.

Upvotes: 0

Views: 2080

Answers (1)

Alex
Alex

Reputation: 11255

You code have a condition for IE:

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

Other code block is a cross-browser and must work in IE:

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

So just remove if/else with IE check.

Upvotes: 1

Related Questions