admin管理员组文章数量:1312912
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
.
本文标签: javascriptWindowprint() does not workStack Overflow
版权声明:本文标题:javascript - Window.print() does not work - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741901583a2403898.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论