是否可以在窗口弹出框中设置标题?在窗口弹出框中设置标题
我有这个在javascript:
var popup = window.open('......');
popup.document.title = "my title";
但这并不work..still看不到任何标题
编辑:网页弹出窗口显示为的.aspx,它有一个标题标签,但仍然无法在弹出窗口中看到..
是否可以在窗口弹出框中设置标题?在窗口弹出框中设置标题
我有这个在javascript:
var popup = window.open('......');
popup.document.title = "my title";
但这并不work..still看不到任何标题
编辑:网页弹出窗口显示为的.aspx,它有一个标题标签,但仍然无法在弹出窗口中看到..
由于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
这不起作用在我的情况下,我有一个URL在window.open –
这不适用于我,我在哪里试图在新标签中打开PDF文件 – Venkat
我在与accepted answer问题,直到我意识到,如果你打开一个现有的,慢页面已经有一个<title>
浏览器将1)设置你的标题,然后2)一旦完全文档加载它将(重新)设置弹出式标题与“正常”值。
所以,引入合理的延迟(功能openPopupWithTitle
):
var overridePopupTitle = function(popup, title, delayFinal, delayRepeat) {
// https://stackoverflow.com/a/7501545/1037948
// 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;
}
试试这个,它会工作。
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);
}
}
这些答案都不适合我。我试图打开一个带有PDF的弹出窗口,并不断尝试使用上述方法设置标题,拒绝了权限。我终于找到了另一篇文章,指出我正确的方向。以下是我最终使用的代码。
来源:How to Set the Title in Window Popup When Url Points to a PDF File
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
}
}
到底是什么 “......”?当你打开一个URL的窗口时,那个页面通常会在标题中用'
这是另一个内部页面..所以可能标题标签在该页面丢失,将检查它 –