Why this does not works? Only loading the page. Not getting printed. Here I take the src of an iframe and open that url in new window.
var myDivObj = document.getElementById('resumedocument').src;
var someXml = '<html><title>Resume</title><body onload="window.print();"><iframe style="height: 1000px; width: 1260px;" src="' + myDivObj + '"/></body></html>';
var printwindow = window.open('', '_blank','fullscreen=yes');
printwindow.document.write(someXml);
printwindow.onload = function() {
printwindow.self.focus();
printwindow.self.print();
};
Why this does not works? Only loading the page. Not getting printed. Here I take the src of an iframe and open that url in new window.
var myDivObj = document.getElementById('resumedocument').src;
var someXml = '<html><title>Resume</title><body onload="window.print();"><iframe style="height: 1000px; width: 1260px;" src="' + myDivObj + '"/></body></html>';
var printwindow = window.open('', '_blank','fullscreen=yes');
printwindow.document.write(someXml);
printwindow.onload = function() {
printwindow.self.focus();
printwindow.self.print();
};
Share
Improve this question
asked Jul 10, 2012 at 4:35
nr.iras.sknr.iras.sk
8,49828 gold badges83 silver badges117 bronze badges
13
- 1 What have you debugged on it? – Cole Tobin Commented Jul 10, 2012 at 4:36
-
1
I would assume that this, for one, is not defined.
document.getElementById('resumedocument').src
– user1479055 Commented Jul 10, 2012 at 4:37 -
1
why does this not work
is not a valid reason for posting on SO. Some work needs to be shown. – Cole Tobin Commented Jul 10, 2012 at 4:38 - 1 Check your browser's javascript console for errors.. – Daedalus Commented Jul 10, 2012 at 4:39
- 4 @ Steve: I accept the answer only if I got the working answer. I think that is the correct way :) – nr.iras.sk Commented Jul 10, 2012 at 4:41
5 Answers
Reset to default 2Replace this line:
printwindow.onload = function() {
printwindow.self.focus();
printwindow.self.print();
};
With this line:
printwindow.document.getElementsByTagName('iframe')[0].onload = function () {
printwindow.self.focus();
printwindow.print();
};
You don't need to do it in onload as you are doing your own document.write. The following code works as an example:
var someXml = '<html><body>Stuff</body></html>';
var printwindow = window.open('', '_blank','fullscreen=yes');
printwindow.document.write(someXml);
printwindow.print();
I think your problem is the onload would fire during the window.open method when the document has been loaded. At this point of time your document is '', so there is no handler.
To wait for the IFRAME to load you need to move the onload handler there. For example:
var someXml = '<html><body><iframe id=Frame width="800" height="800" src="http://jsfiddle" /></body></html>';
var printwindow = window.open('', '_blank','fullscreen=yes');
printwindow.document.write(someXml);
printwindow.document.getElementById('Frame').onload = function () {
printwindow.self.focus();
printwindow.print();
};
var url = document.getElementById('resumedocument').src;
var printwindow = window.open('', '', 'fullscreen=yes');
printwindow.document.write('<iframe width="100%" height="100%" onload="window.print()" src='+url+'></iframe>');
Did you try without the self, as in printwindow.print()
?
Note https://developer.mozilla/en-US/docs/Web/API/Window/print#Notes :
Starting with Chrome 46.0 this method is blocked inside an
<iframe>
unless its sandbox attribute has the valueallow-modals
.