前台打印IFrame中内容的一个函数
                    function doprint() {
                        alert("bbbb");//加入此句,下面print可正常执行,不加此句,便不能正常执行
                        bdhtml = PrintWindow.document.body.innerHTML;
                        sprnstr = "<!--startprint-->";
                        eprnstr = "<!--endprint-->";
                        prnhtml = bdhtml.substr(bdhtml.indexOf(sprnstr) + 17);
                        prnhtml = prnhtml.substring(0, prnhtml.indexOf(eprnstr));
                        PrintWindow.document.body.innerHTML = prnhtml;
                        
                        PrintWindow.document.execCommand('print');
                        PrintWindow.document.body.innerHTML = bdhtml;                    }
后台调用时使用
 ClientScript.RegisterStartupScript(this.GetType(), "js", "doprint();", true); 无法弹出打印窗口,但是在doprint()的开头加入alert("bbbb");后便可正常执行,困惑不已,求解。万分感谢!

解决方案 »

  1.   

    有可能是网页还没有来得及载入完成。在 doucument_complete 里面处理。
      

  2.   

    那是肯定的啊  alert("bbb");就是弹出一个窗口啊, 你点击关闭窗口时应该还会有错的
      

  3.   


    感谢2楼给予的思路,调用前台doprint的时候,iframe中的内容还未加载完毕,因此出错,而加入alert后有了延时,因此能够正确执行,加入判断后问题解决。最终解决代码如下
                        function doprint() {
                            //alert("bbbb");
                            bdhtml = PrintWindow.document.body.innerHTML;
                            sprnstr = "<!--startprint-->";
                            eprnstr = "<!--endprint-->";
                            prnhtml = bdhtml.substr(bdhtml.indexOf(sprnstr) + 17);
                            prnhtml = prnhtml.substring(0, prnhtml.indexOf(eprnstr));
                            PrintWindow.document.body.innerHTML = prnhtml;
                            
                            PrintWindow.document.execCommand('print');
                            PrintWindow.document.body.innerHTML = bdhtml;                    }
                        function test() //判断iframe是否载入完毕,载入完毕后调用doprint
                        {
                            
                            if (PrintWindow.attachEvent) {
                                PrintWindow.attachEvent("onload", function () {
                                    doprint();
                                });
                            } else {
                                PrintWindow.onload = function () {
                                    doprint();
                                };
                            }
                        }结贴给分。