首页 > 解决方案 > 使用 window.open(...) 时带有 Angular 的浏览器选项卡标题

问题描述

在后面的代码中,我打开一个新的浏览器选项卡(窗口),其中包含 PDF 作为内容,来自服务器的 blob 数据。它可以工作,文档会打开一个新选项卡,但浏览器选项卡的标题是一些十六进制代码。我可以动态更改此浏览器选项卡标题吗?例如“文件”?

这是我使用的代码(抱歉,由于数据来自服务器,因此无法进行 StackBlitz 示例):

const file = new Blob([data], {type:'application/pdf'});

const fileURL = URL.createObjectURL(file);

window.open(fileURL, '_blank');

在这里,我的浏览器选项卡看起来像:

浏览器选项卡

解决方案(适用于 Chrome 和 Edge):

var w = window.open(fileURL, '_blank');
setTimeout(function(){ w.document.title = 'My title'; }, 500);

其他解决方案(适用于 Chrome、FF 和 Edge):

var w = window.open(fileURL, '_blank');
w.document.write('<html><head><title>My title</title></head><body height="100%" width="100%"><iframe src="' + fileURL+ '" height="100%" width="100%"></iframe></body></html>');

标签: angulartypescriptweb

解决方案


您是否看过这篇文章如何使用 window.open() 显示窗口标题??

社区建议有几种方法可以实现这一目标


推荐阅读