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

问题描述 投票:0回答:12

我想打开一个新窗口使用:

window.open('<myfile>.pdf','my window','resizable,scrollbars');

新窗口打开,但我没有得到窗口标题为“我的窗口”。 可能出了什么问题?

javascript title window.open
12个回答
45
投票

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

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

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 正文中

<U><A style="cursor: pointer;" onclick="OpenWindow()">Open in New Window</a></U>


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 watch,然后还有一个超时。就我而言,我必须这样编码:

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.

4
投票

JavaScript 的“title”参数是一个在 JavaScript 中使用的变量。写在窗口顶部的实际标题通常来自 HTML

<title>
标签,但由于您显示的是 PDF,所以没有它。


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();
        }

Onclick

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

3
投票

以下代码适用于 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";

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
投票
    var myWindow = window.open('', '', 'width=600,height=400');
    setTimeout(function(){ myWindow.document.title = 'my new title'; }, 1000);

works chrome 2018


1
投票

您可以尝试编写 html 文档:将 pdf url 设置为 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>`);

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

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

0
投票
const myWindow = window.open('<myfile>.pdf', 'my-window');
if (myWindow ) {
    const title = myWindow.document.createElement('title');
    title.innerText = 'my window title';
    myWindow.document.head.append(title);
}
© www.soinside.com 2019 - 2024. All rights reserved.