最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

window.open - Window title for javascript print - Stack Overflow

programmeradmin1浏览0评论

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
 |  Show 2 more ments

3 Answers 3

Reset to default 3

Actually 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.

发布评论

评论列表(0)

  1. 暂无评论