在ie9下面,出现了一个非常头疼的问题。调用window.print()对文档进行打印。但是一直不弹打印对话框。结果我刷新页面,页面加载完成之后,打印对话框自动弹出来了。
function printDiv() { var divToPrint = document.getElementById('printArea'); newWin= window.open(); newWin.document.write(divToPrint.innerHTML); newWin.print(); newWin.close(); }
上面的代码就是我的代码。
出现这样的问题,有一个显著的特点,就是你对document进行了写入操作,即调用了document.write。然后去调用window.print。
解决办法就是:在调用document.write后面,先调用document.close(),然后再去调用print,就可以了。
原因可能在于js调用write之后,就打开了一个流操作,只要没有关闭流操作,其他的系统性的调用,都是不会走的。代码如下
function printDiv() { var divToPrint = document.getElementById('printArea'); newWin= window.open(); newWin.document.write(divToPrint.innerHTML); newWin.document.close(); newWin.focus(); newWin.print(); newWin.close(); }
-----------------------------------------------------
转载请注明来源此处
原地址:#
发表