Configuring the OnLoad event for a newly opened window in IE6

I need to set the onload attribute for a popup. 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'>

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

The goal is to open a new window and call up the print dialog for that window after it opens.

Any tips on how to make this work? It is important not to use javascript anywhere else on the landing page, as I do not control it. All functionality should be contained in the link published above.

+3
source share
6 answers

, , , "printwindow.document.body null ". , IE - window.open(), , , .

- setTimeout . :

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

+4

, . FF, . , :

printwindow=window.open('/mypage.html');
printwindow.onload = function() {
  printwindow.focus();
  printwindow.print();
}
+1

"printwindow.document.body.onload = self.print();" , . , .

: HTML , , , iframe. body.onload = self.print().

0

onload - , , , , HTML- . :

printwindow.onload

JavaScript - . , script :

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

,

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

! URL- "www.google.com". , .

0

( , ) , , setTimeout print() , onload:

<a onclick="self.printwindow=window.open('print.html');setTimeout('self.printwindow.print()',3000);return false;" href='print.html'>
-1

Source: https://habr.com/ru/post/1698835/


All Articles