Window.Print()不能在chrome / Firefox中使用 [英] Window.Print() not working in chrome/Firefox

查看:696
本文介绍了Window.Print()不能在chrome / Firefox中使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下代码在div中打印内容。它在IE中正在下滑,但在chrome和firefox中显示空白屏幕。

I am using the following code to print the content in the div. It is woinking fine in IE but it showing blank screen in chrome and firefox.

代码:

var DocumentContainer = document.getElementById('TermsMainDiv');
    var WindowObject = window.open('', "PrintWindow", 
"width=800,height=700,top=200,left=200,toolbars=no,scrollbars=yes,status=no,resizable=no");
    WindowObject.document.writeln(DocumentContainer.innerHTML);
    WindowObject.document.close();
    WindowObject.focus();
    WindowObject.print();
    WindowObject.close();


推荐答案

分离IE和其他浏览器的代码。现在工作正常。

Separate code for IE and other browsers. It is working fine now.

代码:

 <script type='text/javascript'>
 var originalContents;
    function Print() {

        if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)) {
            var DocumentContainer = document.getElementById('TermsSCMainDiv');
            var WindowObject = window.open('', "PrintWindow", 
"width=800,height=700,top=200,left=200,toolbars=no,
 scrollbars=yes,status=no,resizable=no");
            WindowObject.document.writeln(DocumentContainer.innerHTML);
            WindowObject.document.close();
            WindowObject.focus();
            WindowObject.print();
            WindowObject.close();
        }
        else {
            originalContents = document.body.innerHTML;
            var printable = document.getElementById('TermsSCMainDiv');
            document.body.innerHTML = printable.innerHTML;
            printCoupon();
        }
    }

    function printCoupon() {
        window.print();
        endPrintCoupon();
    }

    function endPrintCoupon() {
        document.body.innerHTML = originalContents;
        document.getElementById('TermsSCMainDiv').scrollIntoView(true);
        location.reload();
    }
 </script>

这篇关于Window.Print()不能在chrome / Firefox中使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆