Setting OnLoad event for newly opened window in IE6

前端 未结 6 457
天涯浪人
天涯浪人 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 06:53

    onload is a property if window objects, not body elements, despite what the HTML attribute might lead you to believe. This is the reference:

    printwindow.onload
    

    However, in this context you can't pass it a string of JavaScript - you need to hand it a function. So, the full script like would look like this

    printwindow.onload=function(){self.print();}
    

    Now, putting it all together

    <a href="www.google.com" onclick="var printwindow=window.open(this.href,'printwindow');printwindow.onload=function(){self.print();};return false;" >try it</a>
    

    HOWEVER! This will not work for you for the URL "www.google.com". The browser security model prevents parent windows from accessing the window objects of child windows, UNLESS they both reside @ the same domain name.

    0 讨论(0)
  • 2021-01-21 06:59

    The final (admittedly, poor) solution that worked for all browsers was to use setTimeout and call print() that way, instead of modifying the onload attribute:

    <a onclick="self.printwindow=window.open('print.html');setTimeout('self.printwindow.print()',3000);return false;" href='print.html'>
    
    0 讨论(0)
  • 2021-01-21 07:07

    This is not possible unless both pages are on the same domain. Your code does not work in FF, but rather prints the current page. If the pages are on the same domain, then you would write:

    printwindow=window.open('/mypage.html');
    printwindow.onload = function() {
      printwindow.focus();
      printwindow.print();
    }
    
    0 讨论(0)
  • 2021-01-21 07:13

    You are relying on "printwindow.document.body.onload=self.print();" line being executed before the child document finishes loading. I don't think you can be guaranteed that.

    Here's an idea: prepare HTML or a page that has nothing but the page you need in an iframe. This page will have body.onload=self.print().

    0 讨论(0)
  • 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

    0 讨论(0)
  • 2021-01-21 07:19

    Check our jQuery. Particularly the document ready feature.

    http://docs.jquery.com/Tutorials:How_jQuery_Works#Launching_Code_on_Document_Ready

    0 讨论(0)
提交回复
热议问题