在窗口弹出窗口中设置标题

Ste*_*fin 7 javascript

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

我在javascript中有这个:

var popup = window.open('......');
popup.document.title = "my title";
Run Code Online (Sandbox Code Playgroud)

但这不起作用..仍然看不到任何标题

编辑:页面弹出窗口显示的是.aspx,它有一个标题标签,但仍然无法在弹出窗口中看到..

pim*_*vdb 17

由于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
Run Code Online (Sandbox Code Playgroud)


drz*_*aus 5

我对接受的答案有疑问,直到我意识到如果您打开一个已经有浏览器的现有慢速页面,<title>浏览器将 1) 设置您的标题,然后 2) 一旦文档完全加载,它将(重新)设置弹出标题与“正常”值。

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

var overridePopupTitle = function(popup, title, delayFinal, delayRepeat) {
    // /sf/answers/525108181/
    // 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;
}
Run Code Online (Sandbox Code Playgroud)