0

可能重复:
Jquery:如何检查新打开的窗口是否已完全加载?/window 加载外部页面(如 www.yahoo.com)

所以我有这个功能,它需要一些html代码,它会打开一个新窗口并将代码放在窗口中,然后提示打印功能。但是,有时,打印 css 没有被加载,并且打印没有构建正确的页面。有什么方法可以查看新打开的窗口是否已经加载了所有内容?

function printer(el)
    {
        print_window= window.open();

        var print_css = '<link rel="stylesheet" href="css/bootstrap.css" type="text/css" />'; 

        print_css = print_css + '<link rel="stylesheet" href="css/main.css" type="text/css" />';
        print_css = print_css + '<link rel="stylesheet" href="css/print.css" type="text/css" media="print" />';


        print_window.document.write("<html><head>" + print_css + "</head><body>" + "<div class='contents'>" + el + "</div>" + "</body> </html>");
        print_window.document.close();
        print_window.focus();
        print_window.print();
        print_window.close();

    }

谢谢

4

1 回答 1

3

这可能会有所帮助——使用window.onload事件处理程序:

print_window.document.close();
print_window.onload = function() {
    print_window.focus();
    print_window.print();
    print_window.close();
};

像往常一样window.onload,在所有链接的图像、脚本和样式表都加载并准备好之前,该事件不会触发。

http://jsfiddle.net/mblase75/3BEkV/

于 2012-12-03T18:27:06.087 回答