检测新打开的窗口是否完全加载了附加的内容

时间:2012-12-03 18:21:35

标签: javascript html

  

可能重复:
  Jquery: how to check if a newly opened window has been fully loaded? / window loads an external page (like 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();

    }

由于

1 个答案:

答案 0 :(得分: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/