knittl
knittl

Reputation: 265261

why can't i set window.onload in IE8?

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.

Upvotes: 0

Views: 740

Answers (2)

Mic
Mic

Reputation: 25164

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>

Upvotes: 3

Tim Down
Tim Down

Reputation: 324597

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.

Upvotes: 1

Related Questions