admin管理员组文章数量:1417070
Why the following code generates TypeError: document.getElementById("docPrint") is null
var printwindow = window.open('', '', 'fullScreen=no');
printwindow.document.write('<iframe id="docPrint" width="100%" height="100%" src="http://localhost:8080/hiring/docs/Keneth _1340800082258/Keneth _resume_1340800082258.pdf"></iframe>');
printwindow.self.focus();
document.getElementById('docPrint').focus();
document.getElementById('docPrint').contentWindow.print();
Why the following code generates TypeError: document.getElementById("docPrint") is null
var printwindow = window.open('', '', 'fullScreen=no');
printwindow.document.write('<iframe id="docPrint" width="100%" height="100%" src="http://localhost:8080/hiring/docs/Keneth _1340800082258/Keneth _resume_1340800082258.pdf"></iframe>');
printwindow.self.focus();
document.getElementById('docPrint').focus();
document.getElementById('docPrint').contentWindow.print();
Share
Improve this question
edited Jul 11, 2012 at 11:50
Esailija
140k23 gold badges279 silver badges328 bronze badges
asked Jul 11, 2012 at 11:48
nr.iras.sknr.iras.sk
8,49829 gold badges83 silver badges117 bronze badges
1
-
4
Did you consider that the element of id
docPrint
does not exist? Because that is most likely what it is. – David G Commented Jul 11, 2012 at 11:49
3 Answers
Reset to default 3You are operating across two windows.
printwindow.document.write
document.getElementById
If you want to get the element you created in the popup then you have to call it's gEBI method.
printwindow.document.write
printwindow.document.getElementById
You need to prefix your calls to document.getElementById with 'printwindow':
printwindow.document.getElementById('docPrint').focus();
printwindow.document.getElementById('docPrint').contentWindow.print();
You might also want to keep a reference to the element in a variable, to avoid the boilerplate
var el = printwindow.document.getElementById('docPrint');
el.focus();
el.contentWindow.print();
Prependprintwindow.
to each instance of document.getElementById
:
printwindow.document.getElementById('docPrint').focus();
printwindow.document.getElementById('docPrint').contentWindow.print();
本文标签: javascriptWhy TypeError documentgetElementById() is nullStack Overflow
版权声明:本文标题:javascript - Why TypeError: document.getElementById() is null - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745258484a2650245.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论