views:

2637

answers:

5

Hey folks- 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'>

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.

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.

A: 

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

buti-oxa
Unfortunately, all I have to work with is this link. I understand your point, though.I attempted to do a "setTimeout('printwindow.print()',3000);", however, it claims printwindow is undefined when I do that. Any ideas?
SocialCensus
A: 

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.

Peter Bailey
Thanks for your concise answer.This works for Firefox, but unfortunately not in IE or Safari. It is as if printwindow.onload is not the proper way to access the onload function for those browsers. Do you have any other advice?
SocialCensus
A: 

Check our jQuery. Particularly the document ready feature.

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

Eli
adding jQuery to the page is outside the scope of this problem.
SocialCensus
+1  A: 

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();
}
John C
A: 

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'>
SocialCensus