js window.open并打印不在谷歌浏览器中工作

时间:2016-03-22 05:52:06

标签: javascript google-chrome

我写的是这样的代码..它在Mozilla Firefox中运行良好,就像在谷歌浏览器中一样,没有在它正在打印的文档中写入任何内容..请帮我解决这个问题。

假设我删除了那个打印语句,那么它工作正常,但我需要打印那个窗口

function prepareInvoice(userDetails, courseId)
        {
            var a = "";
            a = a+"<html><head><title>Invoice | Palle University</title>";
            a = a + "<link href=\"../assets/css/bootstrap.css\" rel=\"stylesheet\" />";
            //form=form+
            a = a + "</head><body><header id=\"header\" class=\"navbar-fixed-top\"> <div class=\"container\" > <div class=\"row\"  ><div class=\"col-lg-6 col-md-6 col-sm-6 col-xs-6 logo-wrapper\">";
                a=a+"<a href=\"#\" style=\"text-decoration:none\"> <div class=\"pulogo\"><label style=\"color:white\">Palle<sup>®</sup></label>University</div></a>";
                a = a + "</div><div style=\"font-weight:bold;\" class=\"col-lg-6 col-md-6 col-sm-6 col-xs-6 text-right\">Invoice</div></div></div></header>";
                a = a + "<section id=\"about\" class=\"clearfix\">";
                a = a + "<div class=\"container\" > <div class=\"row\"  ><div class=\"col-lg-6 col-md-6 col-sm-6 col-xs-6\">";
                a = a + "manish complex;<br>Mangammana palya main road;<br>Bommanahalli-560068;<br>Bangalore;Karnataka-India";
                a = a + "</div><div class=\"col-lg-6 col-md-6 col-sm-6 col-xs-6\">";
                a = a + "<b>Customer Details:</b><br>";
                if (userDetails.Lastname != "")
                    a = a + "Last Name:" + userDetails.Lastname + "<br>";
                if (userDetails.FirstName != "")
                    a = a + "First Name:" + userDetails.FirstName + "<br>";
                if (userDetails.Address != "")
                    a += "address:" + userDetails.Address + "<br>";
                if (userDetails.CityName != "")
                    a += "city:" + userDetails.CityName + "<br>";
                if (userDetails.State != "")
                    a += "state:" + userDetails.State + "<br>";
                if (userDetails.CountryName != "")
                    a += "Country:" + userDetails.CountryName + "<br>";
                if (userDetails.Email != "")
                    a += "email:" + userDetails.Email + "<br>";
                a = a + "</div></div></div>";
                a = a + "<div class=\"container\" > <div class=\"row\"> <div class=\"col-lg-12 col-md-12 col-sm-12 col-xs-12\">";
                a = a + "<table style=\"font-size:15px\" class=\"table table-responsive table-condensed table-striped table-hover no-margin text-center\"><thead><tr class=\"text-center\"><td>Course Name</td><td>Purchased On</td><td>Expires On</td><td>Amount</td></tr></thead><tbody>";
                var cur_symbol='';
                var amount=0;
                for (i = 0; i < userDetails.LstCourses.length;i++)
                {

                    if (courseId == userDetails.LstCourses[i].CourseId) {
                        var monthNames = ["jan", "feb", "march", "april", "may", "june",
                                          "July", "aug", "sep", "oct", "nov", "dec"];
                        a = a + "<tr><td>" + userDetails.LstCourses[i].Course_displayname + "</td>";
                        var d = new Date(parseInt(userDetails.LstCourses[i].Paid_date.substr(6)));
                        var curr_date = d.getDate();
                        var curr_month = monthNames[d.getMonth()];
                        var curr_year = d.getFullYear();
                        var cur_symbol = '';
                        if (userDetails.LstCourses[i].Currency_code.toLowerCase() == "usd")
                            cur_symbol = "usd ";//"<i class=\"fa fa-usd\"></i>";
                        else if (userDetails.LstCourses[i].Currency_code.toLowerCase() == "inr")
                            cur_symbol = "inr ";//"<i class=\"fa fa-inr\"></i>";

                        a = a + "<td>" + curr_date + "-" + curr_month + "-" + curr_year + "</td>";
                        d = new Date(parseInt(userDetails.LstCourses[i].Expire_date.substr(6)));
                        curr_date = d.getDate();
                        curr_month = monthNames[d.getMonth()];
                        curr_year = d.getFullYear();
                        a = a + "<td>" + curr_date + "-" + curr_month + "-" + curr_year + "</td>";
                        amount = userDetails.LstCourses[i].Amount_paid;
                        a = a + "<td>" + cur_symbol + userDetails.LstCourses[i].Amount_paid + "</td></tr>";//display dollar or inr symbol.
                        //display the total amount based on the course id and a thank you ! message . 
                    }
                }
                a = a + "<tr><td></td><td></td><td></td><td>Total: " + cur_symbol +" "+ amount + "</td></tr>";
                a = a + "</tbody></table></div></div></div>";
                a = a + "<div class=\"container\" > <div class=\"row\"> <div class=\"col-lg-12 col-md-12 col-sm-12 col-xs-12 text-center\">";
                a = a + "Thank you !</div></div></div>";
                //a = a + "<table><tr><td></td><td></td><td></td><td>Total:" + cur_symbol + amount + "</td></tr></table></div></div></div>";
                a = a + "</body></html>";
                //a.print();oUser.Lastname; oUser.Lastname;oUser.Address;
            //oUser.Email; oUser.CityName; oUser.State; oUser.CountryName;
                debugger;
                var mywindow = window.open('', 'my div', 'height=400,width=600');
                mywindow.document.write(a);
                mywindow.document.close();
                mywindow.focus();
                mywindow.print();
        } 

1 个答案:

答案 0 :(得分:0)

尝试使用div内容

function printDivision() {
var printContents = document.getElementById('divId').innerHTML;
var originalContents = document.body.innerHTML;
document.body.innerHTML = "<div align='center' style='padding: 5%;' >"
        + printContents + "</div>";
window.print();
document.body.innerHTML = originalContents;
}