print() not working on opera browser

半腔热情 提交于 2019-12-03 22:23:11

问题


I am trying to open a print dialog box in Opera browser using javascript. The print() is working fine in all browsers but in opera it doesn't work. My print() code as,

var printContent = document.getElementById(elementId);
var windowUrl = url;
var uniqueName = new Date();
var windowName = 'Print' + uniqueName.getTime();
var printWindow = window.open(windowUrl, windowName, 'scrollbars=yes');
printWindow.document.write(printContent.innerHTML);
printWindow.document.close();
printWindow.focus();
printWindow.print();

I want to open print dialog box in Opera browser. Can anyone help me?


回答1:


In Opera, the print dialog will only display if the page has loaded (see the link in cjrh's comment), so maybe the window you're dynamically opening has not finished loading when you try to print it. Try replacing this line:

printWindow.print();

with this line:

printWindow.onload = printWindow.print;



回答2:


I had the same problem and this was the only solution that actually worked for me:

window.addEventListener('load', function(e) { window.print(); }, false);

Version 11.60
Build 1185
Platform Win32
System Windows XP




回答3:


I think there are two things happening here:

  1. As others have pointed out, Opera requires that window.print() be triggered by an event: a click or a load event, for example. In general, you'll need to wrap window.print() in an event listener. Try window.addEventListener('load', function(e) { window.print(); }, false); or window.onload = function(){ window.print() }

  2. It looks like Opera also only allows a limited subset of window methods on windows that are opened via JavaScript. print() doesn't appear to be one of the methods allowed.




回答4:


I solved this issue via jQuery with this very simple function instead ofissue the print one which you can replace on your code and will satisfy your needs for Opera and the rest of the browsers.

  $(document).ready(function(){
    if($.browser.opera){
     window.print();
    }
    else printWindow.print();
  });


来源:https://stackoverflow.com/questions/4582767/print-not-working-on-opera-browser

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!