javascript - Chrome 中的打印问题

标签 javascript jquery google-chrome printing-web-page

我的 Google Chrome 版本为 34.0.1847.116。 我使用以下代码进行打印

$('#print').live("click", function(){
var url = baseUrlReports + "******************";

var txnwindow = window.open(url ,"");
txnwindow.print();

txnwindow.focus();

});

此时,Chrome 卡在“加载预览”处。然后我的所有 JS 都停止响应,并且我无法刷新页面。没有其他选项可以关闭窗口。谁能告诉我这里到底是什么问题?相同的代码在 FireFox 和 IE 上运行良好。

最佳答案

好吧,我也遇到了同样的问题

对于那些有兴趣查看它的人 - 您可以在这里找到并复制它: http://jsfiddle.net/pvkovalev/vGr7Y/ (只需关闭 Chrome 中的打印弹出窗口,不要使用“打印”或“关闭”按钮,只需关闭窗口本身即可)

这是我的解决方案(我基本上不会在 Chrome 中调用 window.print())

    //Begin browser detection section
    var isOpera = !! window.opera || navigator.userAgent.indexOf(' OPR/') >= 0;
    // Opera 8.0+ (UA detection to detect Blink/v8-powered Opera)
    var isFirefox = typeof InstallTrigger !== 'undefined'; // Firefox 1.0+
    var isSafari = Object.prototype.toString.call(window.HTMLElement).indexOf('Constructor') > 0;
    // At least Safari 3+: "[object HTMLElementConstructor]"
    var isChrome = !! window.chrome && !isOpera; // Chrome 1+
    var isIE = false || !! document.documentMode; // At least IE6
        //End browser detection section

        //User click on something
    $('a[href="#print"]').on("click", function () {
        var data = "<div> a lot of HTML code from AJAX call </div>";
                CreateWindow(data);
    });


    function CreateWindow(data) {
        var mywindow = window.open('about:blank', '_blank');
        var myWindowContents = '<html>' +
            '<head>' +
            '<title>Results</title>' +
            '<link href="http://netdna.bootstrapcdn.com/bootstrap/3.0.3/css/bootstrap.min.css" rel="stylesheet" type="text/css">' +
            '</head>' +
            '<body >' + data +
            '<script src="http://code.jquery.com/jquery-2.1.0.min.js type="text/javascript">' + '<' + '/script>' +
            '<script type="text/javascript">' +
            '$(document).ready(function () {' +
            '});' +
            '<' + '/script>' +
            '</body>' +
            '</html>';
        mywindow.document.write(myWindowContents);
        mywindow.document.close();
        mywindow.focus();
        if (!isChrome) {
            mywindow.print();
        }
    }

您还可以在此处查看我的代码: http://jsfiddle.net/pvkovalev/2pn4p/

这是迄今为止我能找到的最好的解决方法。

关于javascript - Chrome 中的打印问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23261536/

相关文章:

javascript - 为什么这个文件上传拖/放不起作用,我完全复制了 CodePen 代码?

javascript - react 模态动态调整大小

javascript - 数据表 - 同一页面上有多个表,但只有一个正在响应

javascript - Jquery 在 ajaxComplete 上调用相同的函数并准备好了吗?

javascript - 检测 Google Chrome 中所有打开的页面,而不仅仅是当前窗口中的页面

javascript - 如何为不写入文件的 grunt 插件编写单元测试?

javascript - 使用ajax将数据从php发送到图表

jquery - table.row 不是函数

html - Google Chrome 浏览器的 HTML、CSS 中的超链接问题

google-chrome - 如果在 Shadow-dom 中提供样式,则鼠标双击时的文本选择不起作用