问题
I have Google Chrome with the version 34.0.1847.116. I am using the following code for printing
$('#print').live("click", function(){
var url = baseUrlReports + "******************";
var txnwindow = window.open(url ,"");
txnwindow.print();
txnwindow.focus();
});
At this moment the Chrome hangs at the Loading preview. Then All of my JS stops responding and I am unable to refresh the page. Have no other option to close the Window. Can anyone tell me what is exactly the problem here? The same code is working good on FireFox and IE.
回答1:
Okay, so I've got the same issue
For those of you who is interested to see it - you can find and reproduce it here: http://jsfiddle.net/pvkovalev/vGr7Y/ (just close the print pop-up in Chrome, DON'T use the buttons "print" or "close", just close a window itself)
So here is my solution (I'm basically not calling window.print() in Chrome)
//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();
}
}
You also can look at my code here: http://jsfiddle.net/pvkovalev/2pn4p/
It's the best workaround which I was able to find so far.
来源:https://stackoverflow.com/questions/23261536/printing-issue-in-chrome