【问题标题】:Setting the page title of Chrome window.open does not work设置Chrome window.open的页面标题不起作用
【发布时间】:2016-11-10 06:07:11
【问题描述】:

我有一些 JavaScript 用于打开一个新窗口以打印部分页面我正在使用 window.open 函数,并且我已将页面标题设置为 window.open 第二个参数(参见下面的代码),但标题出现 'about:blank' 而不是 'test'

<script type="text/javascript">
    function printPartOfPage(elementId) {
        var printContent = document.getElementById(elementId);
        var windowUrl = 'about:blank';
        var printWindow = window.open(windowUrl, 'test', 'left=50000,top=50000,width=0,height=0');


        printWindow.document.write(printContent.innerHTML);
        printWindow.document.close();
        printWindow.focus();
        printWindow.print();
        printWindow.close();
    }
</script>

注意: 我知道这个问题可能会重复,但我尝试了所有对我不起作用的答案喜欢:

1-将open()的第一个参数设置为'about:blank'

2- printWindow.document.title = 'test'

结果如下图

【问题讨论】:

  • 你的 chrome 版本是什么?
  • 版本 54.0.2840.71 m
  • 你能截屏一下你的弹出窗口的结果吗
  • 我通过在结果中添加屏幕截图来编辑我的问题
  • 你把printWindow.document.title放在哪一行?

标签: javascript window.open


【解决方案1】:

问题是你放的

printWindow.document.title = 'title'

printWindow.document.write之前
解决方案放在printWindow.document.title 之后printWindow.document.write

<script type="text/javascript">
    function printPartOfPage(elementId) {
        var printContent = document.getElementById(elementId);
        var windowUrl = 'about:blank';
        var printWindow = window.open(windowUrl, 'test', 'left=50000,top=50000,width=0,height=0');


        printWindow.document.write(printContent.innerHTML);
        printWindow.document.title = 'This is the title'
        printWindow.document.close();
        printWindow.focus();
        printWindow.print();
        printWindow.close();
    }
</script>

Fiddle Here

【讨论】:

  • 非常感谢,我花了很长时间才找到 Sol
  • @BentEl-Eslam 如果此答案解决了您的问题,只需将其标记为检查。
【解决方案2】:

您可以将data URI 替换为.html document&lt;title&gt; 标签替换about:blankdocument.write()。在&lt;script&gt;标签集内调用print()作为&lt;body&gt;元素的最后一个子元素。

function printPartOfPage(elementId) {
  var printContent = document.getElementById(elementId);
  var windowUrl = "data:text/html,<!DOCTYPE html><html><head><title>test</title></head><body>" + printContent.innerHTML + "<script>print()<\/script></body></html>";
  var printWindow = window.open(windowUrl, "test", 'left=50000,top=50000,width=0,height=0');
}

plnkrhttp://plnkr.co/edit/ZfqHgNnjAkunJeryn71Y?p=preview

使用Blob()URL.createObjectURL()window.open() URL 设置为Blob URL

function printPartOfPage(elementId) {
  var printContent = document.getElementById(elementId);
  var html = "<!DOCTYPE html><html><head><title>test</title></head><body>" + printContent.innerHTML + "<script>print()<\/script></body></html>";
  var blob = new Blob([html], {type:"text/html"});
  var windowUrl = URL.createObjectURL(blob);
  var printWindow = window.open(windowUrl, "test", 'left=50000,top=50000,width=0,height=0');
  window.onfocus = function() {
    URL.revokeObjectURL(windowUrl);
    printWindow.close();
  }
}

【讨论】:

  • Pop-upsSettings 设置是什么?有没有点击&lt;h1&gt;文字内容"test"
  • 不,它适用于我的答案,但是当将 printPartOfPage 更改为您的时它不会出现
  • 允许所有网站显示弹出我不明白的第二个问题
  • 允许 plnkr origin 打开弹出窗口。你检查过Pop-upsSetiings吗?当前的浏览器弹出偏好设置是什么?
  • 抱歉,设置中的弹出窗口部分只有 2 个单选按钮允许或不允许网站显示弹出窗口,我可以在其中找到“浏览器弹出首选项”
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-08-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多