在IE6中为新打开的窗口设置OnLoad事件 [英] Setting OnLoad event for newly opened window in IE6

查看:102
本文介绍了在IE6中为新打开的窗口设置OnLoad事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要为新弹出的窗口设置onload属性。
以下代码适用于Firefox:

I need to set the onload attribute for a newly popped-up window. The following code works for Firefox:

<a onclick="printwindow=window.open('www.google.com');printwindow.document.body.onload=self.print();return false;" href='www.google.com'>

然而,当我在IE中尝试这个时,我收到一个错误 - printwindow.document.body null或未定义'

However, when I try this in IE, I get an error - "printwindow.document.body null or not defined'

目标是弹出一个新窗口,并在打开该窗口时调出该窗口的打印对话框。

The goal is to pop open a new window, and call up the print dialog for that window once it's been opened.

有关如何使其工作的任何线索?
重要的是不要在目标页面上的其他地方使用javascript,因为我无法控制它。所有功能必须包含在上面发布的链接。

Any clues on how to make this work? It is important not to use javascript elsewhere on the target page, as I do not have control over it. All functionality must be contained in the link I posted above.

推荐答案

虽然前面的答案正确地说明了新窗口必须来自同一个域,但他们错误地回答了为什么他得到错误'printwindow.document.body null或not defined'。这是因为IE没有从window.open()返回任何状态,这意味着你可以打开一个页面,并尝试在onload可用之前访问它。

While earlier answers correctly stated the new window must be from the same domain, they incorrectly answered why he got the error 'printwindow.document.body null or not defined'. It's because IE doesn't return any status from window.open() which means you can open a page and try to access it BEFORE onload is available.

因此你需要使用类似setTimeout的东西来检查。例如:

For this reason you need to use something like setTimeout to check. For example:

printwindow = window.open('print.html');
var body;
function ieLoaded(){
    body = printwindow.document.getElementsByTagName("body");
    if(body[0]==null){
        // Page isn't ready yet!
        setTimeout(ieLoaded, 10);
    }else{
        // Here you can inject javascript if you like
        var n = printwindow.document.createElement("script");
        n.src = "injectableScript.js";
        body.appendChild(n);

        // Or you can just call your script as originally planned
        printwindow.print();
    }
}
ieLoaded();

这是进一步讨论的这里

这篇关于在IE6中为新打开的窗口设置OnLoad事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆