在窗口弹出中设置标题

8

在弹出窗口中设置标题是否可能?

我有以下javascript代码:

var popup = window.open('......');
popup.document.title = "my title";

但这并没有起作用...仍然看不到任何标题。
编辑:弹出的页面是.aspx格式的,它有一个标题标签,但仍然看不到弹出窗口上的标题。

2
“......”到底是什么?当您打开一个指向URL的窗口时,该页面通常会在标题中使用头部的<title>属性进行设置。这个URL是否来自您的域名? - Pointy
这是另一个内部页面。所以该页面可能缺少标题标签,我会检查一下。 - Stewie Griffin
8个回答

22

由于popup.onload似乎无法正常工作,这里有一个解决方法:http://jsfiddle.net/WJdbk/

var win = window.open('', 'foo', ''); // open popup

function check() {
    if(win.document) { // if loaded
        win.document.title = "test"; // set title
    } else { // if not loaded yet
        setTimeout(check, 10); // check in another 10ms
    }
}

check(); // start checking

1
这在我的情况下不起作用,因为我在window.open中有一个URL。 - Sahil Jain
1
这对我不起作用,我试图在新标签页中打开PDF文件。 - Venkat

5
我曾经在使用被接受的答案时遇到了问题,直到我意识到如果你打开一个已经存在且速度较慢的页面,其中已经有了<title>标签,浏览器将首先设置您的标题,然后在文档完全加载后重新设置弹出窗口标题为“正常”值。

因此,引入合理的延迟(函数openPopupWithTitle):

var overridePopupTitle = function(popup, title, delayFinal, delayRepeat) {
    // https://dev59.com/2FvUa4cB1Zd3GeqPuH0A#7501545
    // delay writing the title until after it's fully loaded,
    // because the webpage's actual title may take some time to appear
    if(popup.document) setTimeout(function() { popup.document.title = title; }, delayFinal || 1000);
    else setTimeout(function() { overridePopupTitle(popup, title); }, delayRepeat || 100);
}
var openPopupWithTitle = function(url, title, settings, delay) {
    var win = window.open(url, title, settings);
    overridePopupTitle(win, title, delay);
    return win;
}

2

这些答案都对我没用。我试图打开一个包含PDF的弹窗,但使用上述方法设置标题时一直出现权限被拒绝的错误。最后我找到了另一篇帖子,指导我正确地完成了操作。以下是我最终使用的代码。

来源: 如何在URL指向PDF文件时设置窗口弹出框的标题

    var winLookup;
    var showToolbar = false;

    function openReportWindow(m_title, m_url, m_width, m_height)
    {
        var strURL;
        var intLeft, intTop;

        strURL = m_url;

        // Check if we've got an open window.
        if ((winLookup) && (!winLookup.closed))
            winLookup.close();

        // Set up the window so that it's centered.
        intLeft = (screen.width) ? ((screen.width - m_width) / 2) : 0;
        intTop = (screen.height) ? ((screen.height - m_height) / 2) : 0;

        // Open the window.
        winLookup = window.open('', 'winLookup','scrollbars=no,resizable=yes,toolbar='+(showToolbar?'yes':'no')+',height=' + m_height + ',width=' + m_width + ',top=' + intTop + ',left=' + intLeft);
        checkPopup(m_url, m_title);

        // Set the window opener.
        if ((document.window != null) && (!winLookup.opener))
            winLookup.opener = document.window;

        // Set the focus.
        if (winLookup.focus)            
            winLookup.focus();
    }

    function checkPopup(m_url, m_title) {     
        if(winLookup.document) { 
            winLookup.document.write('<html><head><title>' + m_title + '</title></head><body height="100%" width="100%"><embed src="' +m_url + '" type="application/pdf" height="100%" width="100%" /></body></html>');
        } else { 
            // if not loaded yet
            setTimeout(checkPopup(m_url, m_title), 10); // check in another 10ms
        }
    }

也许我漏掉了什么,但是 setTimeout 不是需要一个函数作为参数吗?而你传递的是 checkPopup 函数调用的结果?如果 checkPopup 没有参数,你可以将 checkPopup 传递给 setTimeout。或者你可以将 "function(){checkPopup(m_url, m_title)}" 传递给 setTimeout。 - Robert

1
不确定这是否有帮助,

function GetInput() {
  var userInput;
  var stringOutput;
  userInput = prompt('What should the title be?', "");
  stringOutput = userInput;
  document.title = stringOutput;
}
<button type="button" onclick="GetInput()">Change Title</button>


1
你也可以使用

标签。

var popup = window.open('......');
popup.onload = function () {
popup.document.title = "my title";
}

0

最终我在我的弹出窗口中创建了一个setTitle方法,并从父页面调用它。

//popup page:
function setTitle(t) {
    document.title = t;
}

//parent page
popupWindow.setTitle('my title');

0
var win= window.open('......');
win.document.writeln("<title>"+yourtitle+"</title>");

这对我有效,在Chromium浏览器中测试过。


-1

试试这个,它会起作用的。

var timerObj, newWindow;

function openDetailsPopUpWindow(url) {
    newWindow = window.open(url, '', 'height=500,width=700,menubar=no,resizable=yes,scrollbars=yes');
    timerObj = window.setInterval("fun_To_ReTitle('~~newTitle~~ ')", 10);
}


function fun_To_ReTitle(newTitle){
    if (newWindow.document.readyState == 'complete') {
        newWindow.document.title=newTitle;
        window.clearInterval(timerObj);
    }
}

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