views:

46

answers:

2

i have the following function, which works fine in firefox, but will do nothing in IE8

function print(url) {
    var w = window.open(url);
    w.onload = function() {
        w.print();
    };
}

i want to open a webpage and immediately open the print dialogue.

+1  A: 

In general, you can't handle the load event of window objects outside the current window because it doesn't work in all browsers. You need to handle the load event using a script in the document loaded into the new window. Your handler could either call window.print() itself, or, if you need to keep the functionality within the original window, it could make a call to the original window using the window.opener property or set a flag that script in the original window can poll.

Tim Down
+2  A: 

If you have the hand on the page at url, place the call there. In the HTML, at the end of the BODY tag. Add a SCRIPT tag with your call

<body>
  ...
  <script>
    window.print();
  </script>
</body>
Mic