如何使用window.open()显示窗口标题?

36

我想使用以下代码打开一个新窗口:

window.open('<myfile>.pdf','my window','resizable,scrollbars');
新窗口已经打开,但是我没有得到窗口的标题为“my window”。 可能出了什么问题?

12
window.open() 的第二个参数是新窗口的 名称,而不是它的标题。实际的标题来自于加载到该窗口中的 HTML 文档的 <title> 元素。由于您正在加载一个 PDF 文件,因此其标题将由您的浏览器自行决定。 - Frédéric Hamidi
1
有没有什么变通方法?我真的需要获取打开 PDF 的新窗口的标题。 - user811433
1
除了为您想要支持的每个浏览器编写插件之外,我不知道任何解决方法。 - Frédéric Hamidi
4
窗口名称不能包含空格,否则IE无法弹出弹窗窗口。 :) - epascarello
12个回答

45

如果域名相同,那么你可以更改新窗口的标题

 <script type="text/javascript">
    var w = window.open('http://localhost:4885/UMS2/Default.aspx');
    w.document.title = 'testing';
 </script>

6
尝试加入一个像这样的 setTimeoutsetTimeout(() => w.document.title = '这是一个测试', 0);。该操作会延迟执行,将网页标题更改为“这是一个测试”。 - Netsi1964
setTimeout()很有帮助,只需将延迟时间从0毫秒改为增加50毫秒即可。 - Avatar

18

这是我的解决方案,请查看:

var myWindow = window.open('<myfile>.pdf','my window','resizable,scrollbars');
myWindow.document.write('<title>My PDF File Title</title>');
希望我能帮上忙。

7
这是我所做的事情:
    <script type="text/javascript">
    function OpenWindow() {
            var pdf = '<%= "PDFs/13.7/" + ddlLinkValidation.SelectedValue.ToString() + ".pdf" %>';
            var win = window.open('','UATRpt', 'menubar=0,location=0,toolbar=0,resizable=1,status=1,scrollbars=1');

            if(win.document) { 
                win.document.write('<html><head><title>Your Report Title</title></head><body height="100%" width="100%"><iframe src="' + pdf + '" height="100%" width="100%"></iframe></body></html>');
            } 
            return true;
    } 
    </script>

在HTML中的body标签下:
<U><A style="cursor: pointer;" onclick="OpenWindow()">在新窗口打开</a></U>


它可以设置页面的标题,但也会将页面的URL设置为空,因此下载链接无法工作。有什么建议吗? - Amit Joshi

5
JavaScript中的"title"参数是一个在JavaScript内部使用的变量。通常在窗口顶部显示的实际标题来自HTML的<title>标签,但由于你正在显示PDF,因此你没有这个标签。

有没有办法可以获取新窗口的标题? - user811433

5
如果新打开的窗口含有一个文件(例如PDF)作为URL,那么页面可能没有“head”标签。您需要先添加一个标签,然后再进行修改/添加标题。
jQuery:
var w = window.open('/path/to/your/file.pdf');// or any url
$(w.document).find('html').append('<head><title>your title</title></head>');

原生JS:

var w = window.open('/path/to/your/file.pdf');// or any url
w.document.getElementsByTagName('html')[0]
   .appendChild(document.createElement('head'))
   .appendChild(document.createElement('title'))
   .appendChild(document.createTextNode('your title'));

现在,如果页面加载时间过长,您可以添加一个 onload 监听器,然后再添加一个超时时间。在我的情况下,我必须编写如下代码:

var w = window.open('/path/to/your/file.pdf');// or any url
w.onload = function(){
    setTimeout(function(){
       $(w.document).find('html').append('<head><title>your title</title></head>');
    }, 500);
} // quite ugly hu !? but it works for me.

关于jQuery,为了保留$(...)对象,我应该选择$(w.document.lastChild)而不是指定.find方法——这样会更快。 - Brice Coustillas

4

更改新打开窗口中 PDF 的标题

    function titlepath(path,name){

        //In this path defined as your pdf url and name (your pdf name)

            var prntWin = window.open();
            prntWin.document.write("<html><head><title>"+name+"</title></head><body>"
                + '<embed width="100%" height="100%" name="plugin" src="'+ path+ '" '
                + 'type="application/pdf" internalinstanceid="21"></body></html>');
            prntWin.document.close();
        }

点击事件

<a onclick="titlepath('your url','what title you want')">pdf</a>

2
以下代码在Mozilla Firefox,IE 11和Google Chrome上对我有效。
var winUrl = 'target URL that needs to open in new window';     

var _newWindow = window.open(winUrl, "_newWindow");

_newWindow.document.title = "My New Title";

这似乎是完成任务最简单的方法。有时候显而易见的就是显而易见的,复杂只会浪费时间。 - Cuse70

1
在我的情况下,唯一可行的方法是像这样使用setTimeout
var mapWin = window.open('', '_blank', ''); // Opens a popup   

setWindowTitle(mapWin) // Starts checking

function setWindowTitle(mapWin)
{
    if(mapWin.document) // If loaded
    {
        mapWin.document.title = "Oil Field Map";
    }
    else // If not loaded yet
    {
        setTimeout(setWindowTitle, 10); // Recheck again every 10 ms
    }
}

1
const wnd = window.open(url, '_blank');

wnd.onload = function() {
   wnd.document.title = 'YOUR TITLE';
}

我喜欢这种易于听懂的方式。 - Penguin

0
你可以尝试编写HTML文档:将PDF网址设置为iframe,让标题标签定义页面标题。
var w = window.open();
w.document.write(`<html>
  <head>
    <title>${title}</title>
  </head>
  <body style="margin: 0; padding: 0">
    <iframe src="${pdfInlineUrl}" style="width: 100%; height: 100%; margin: 0; padding: 0; border: none;"></iframe>
  </body>
</html>`);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接