JavaScript 打印的窗口标题
Window title for javascript print
找到这段代码来打印我修改的javascript元素。 尽管我添加了document.title
和<title></title>
标签,但在常规文本编辑器中打开的窗口显示 untitled
.
这通常是保存文本之前的情况。 无论如何,有没有办法显示标题?
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);
实际上原始代码也对我有用(Chrome,没有在其他浏览器上测试)
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);
请参阅JSFiddle上
我的猜测是分配newWin.document.title = "Readings on PageLinks";
失败,因为当时页面中没有<title>
元素。
因此,newWin.document.title
仍然没有定义。然后你把它连接到字符串<title>'+newWin.document.title+'</title>
,所以它被toString()
-ed为"undefined"。
因此,只需将标题直接写入字符串即可
newWin.document.write('<html><head><title>Readings on PageLinks</title>...');
正如Eran Medan在评论中建议的那样。
这对我有用。
这可能是随文档打开的编辑器的行为。 在保存文档之前,编辑器标题将显示"无标题"。 这一定是设计使然。
相关文章:
- 打开新窗口进行打印时不显示 JavaScript 图像
- 在另一个选项卡中打开同一域时,窗口标题会发生更改
- 使用javascript在HTML的输出窗口中打印控制台消息
- 如何设置弹出窗口标题
- JavaScript 打印的窗口标题
- 如何在科尔多瓦应用程序中摆脱窗口标题栏
- 禁用 Satellizer 弹出窗口标题栏
- 如何在网页中设置窗口标题
- 尝试更改浏览器窗口标题时权限被拒绝
- 在新窗口中打印Javascript赢得't显示图像
- 允许窗口在打印前进行格式化
- 两个Bootstrap弹出窗口标题在一个单一的弹出窗口
- 将HTML页面加载到新窗口并打印
- 页面完全加载后,更改Firefox浏览器的窗口标题
- 如何在自定义高度和宽度的新弹出窗口上更改窗口标题
- 将主窗口标题改为iframe标题
- 如何在单独的窗口中打印html表格
- 点击链接后,从Twitter.com获取窗口标题
- 文档.写入不会在浏览器窗口上打印
- 尝试在新窗口中打印内容