2

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?

ismail
  • 46,010
  • 9
  • 86
  • 95
Jayashri
  • 4,073
  • 6
  • 25
  • 18
  • Duplicate of http://stackoverflow.com/questions/3482428/window-print-not-working-with-opera-browser perhaps? – Caleb Hattingh Jan 03 '11 at 09:28
  • Which Version of Opera are you using? What error message if any do you get in the console? print() in general works in Opera (try using it in the command line in Opera Dragonfly for example.) – David Storey Jan 10 '11 at 18:37
  • this seems to be an issue with window.open() I will check further. – karlcow Jan 10 '11 at 22:00
  • I am using Opera 12 version. If I used window.print() instead of printWindow.print(); then its working on opera but it will not working on other browsers. – Jayashri Jan 14 '11 at 13:59

4 Answers4

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;
tagawa
  • 4,561
  • 2
  • 27
  • 34
1

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

Aliaksei Kliuchnikau
  • 13,589
  • 4
  • 59
  • 72
0

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.

webinista
  • 3,750
  • 1
  • 20
  • 21
0

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();
  });
Peter O.
  • 32,158
  • 14
  • 82
  • 96
Jean G.T
  • 1
  • 10
  • 25