Setting OnLoad event for newly opened window in IE6

前端 未结 6 505
天涯浪人
天涯浪人 2021-01-21 06:40

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



        
6条回答
  •  粉色の甜心
    2021-01-21 07:18

    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.

    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();
    

    This is further discussed here

提交回复
热议问题