Found this code to print a javascript element that I modified. Although I added the document.title
and the <title></title>
tags, the window that opens in the regular text editor says untitled
.
This is usually the situation before the text is saved. Is there a way to show a title anyway?
var element=document.getElementById(element_id);
var newWin=window.open('','Print-Window','width=400,height=400,top=100,left=100');
newWin.document.open();
newWin.document.title = "Readings on PageLinks";
newWin.document.write('<html><head><title>'+newWin.document.title+'</title></head><body onload="window.print()">'+element.innerHTML+'</body></html>');
newWin.document.close();
setTimeout(function(){ newWin.close(); },10);
Found this code to print a javascript element that I modified. Although I added the document.title
and the <title></title>
tags, the window that opens in the regular text editor says untitled
.
This is usually the situation before the text is saved. Is there a way to show a title anyway?
var element=document.getElementById(element_id);
var newWin=window.open('','Print-Window','width=400,height=400,top=100,left=100');
newWin.document.open();
newWin.document.title = "Readings on PageLinks";
newWin.document.write('<html><head><title>'+newWin.document.title+'</title></head><body onload="window.print()">'+element.innerHTML+'</body></html>');
newWin.document.close();
setTimeout(function(){ newWin.close(); },10);
Share
Improve this question
asked May 9, 2012 at 19:44
user823527user823527
3,71217 gold badges69 silver badges111 bronze badges
7
- 1 why not: newWin.document.write('<html><head><title>Readings on PageLinks</title>... ? – Eran Medan Commented May 9, 2012 at 19:46
- That did not make a difference. I had that first. – user823527 Commented May 9, 2012 at 19:48
- you must of had something different or made a mistake somewhere, as Eran is correct on that, at least for my puter – Huangism Commented May 9, 2012 at 19:58
- What browser are you testing this on? – Eran Medan Commented May 9, 2012 at 20:05
-
I was testing this on Safari. Like I said I had
<head><title>Readings on PageLinks</title></head>
and that did not make a difference. – user823527 Commented May 9, 2012 at 22:06
3 Answers
Reset to default 3Actually the original code worked for me as well (Chrome, didn't test on other browsers)
var element_id = "id1";
var element = document.getElementById(element_id);
var newWin = window.open('', 'Print-Window', 'width=400,height=400,top=100,left=100');
newWin.document.open();
newWin.document.title = "Readings on PageLinks";
newWin.document.write('<html><head></head><body onload="window.print()">' + element.innerHTML + '</body></html>');
newWin.document.close();
setTimeout(function() {
newWin.close();
}, 10);
See on JSFiddle
My guess is that the assignment newWin.document.title = "Readings on PageLinks";
failed, because there was no <title>
element in the page at that time.
Thus, newWin.document.title
was still undefined. Then you concatenated it to the string <title>'+newWin.document.title+'</title>
, so it got toString()
-ed as "undefined".
So, just write the title directly into the string
newWin.document.write('<html><head><title>Readings on PageLinks</title>...');
as Eran Medan suggested in the ments.
This worked for me.
It could be a behavior of the editor that's opened with the document. Until the document is saved, the editor header will say "untitled". This must be by design.