window.print not working with opera browser

2019-03-02 18:31发布

问题:

I am trying to open a print dialog box in Opera browser using javascript code as

<script language=javascript>
window.print(); //This is working in IE, Netscape, Firefox, but not working in Opera
</script>

where as if I am using the following code Opera browser understands and able to open print dialog box

<input type="button" value="Print this page" onClick="javascript:window.print();" ID="Button1" NAME="Button1">

My requirement is to open print dialog box in Opera browser using script block. Can anyone help me?

回答1:

Try putting your code in load event:

<script language=javascript>
window.onload = function(){
  window.print();
};
</script>


回答2:

You need to make sure the whole web page is loaded in Opera before you call window.print();

So using this may help - not, I am waiting for the page to load AND causing a slight delay, which is a little known fix for some versions of Opera.

Hope it helps.

window.onload = function () {
    window.setTimeout(function () {
        window.print();
    }, 500);
}