Javascript 在弹出窗口中设置标题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7501424/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Set title in the window popup
提问by Stewie Griffin
Is it possible to set a title in the window popup?
是否可以在弹出窗口中设置标题?
I have this in javascript:
我在javascript中有这个:
var popup = window.open('......');
popup.document.title = "my title";
but this does not work..still can't see any title
但这不起作用..仍然看不到任何标题
EDIT: the page popup is displaying is .aspx and it HAS a title tag, but still can't see that on the popup window..
编辑:显示的页面弹出窗口是 .aspx 并且它有一个标题标签,但在弹出窗口上仍然看不到。
回答by pimvdb
Since popup.onload
does not seem to work, here is a workaround: http://jsfiddle.net/WJdbk/.
由于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
回答by drzaus
I was having problems with the accepted answeruntil I realized that if you open an existing, slowpage that already has a <title>
the browser will 1) set your title, then 2) once the document fullyloads it will (re)set the popup title with the "normal" value.
我对接受的答案有问题,直到我意识到如果你打开一个现有的、已经有一个缓慢的页面,<title>
浏览器将 1) 设置你的标题,然后 2) 一旦文档完全加载它会(重新)设置弹出标题与“正常”值。
So, introducing a reasonable delay (function openPopupWithTitle
):
因此,引入合理的延迟(函数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;
}
回答by George
None of these answers worked for me. I was trying to open a popup with a PDF inside and kept getting permission denied trying to set the title using the above methods. I finally found another post that pointed me in the correct direction. Below is the code I ended up using.
这些答案都不适合我。我试图打开一个带有 PDF 的弹出窗口,并一直被拒绝尝试使用上述方法设置标题。我终于找到了另一篇文章,指出了正确的方向。下面是我最终使用的代码。
Source: How to Set the Title in Window Popup When Url Points to a PDF File
来源:当 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
}
}
回答by Random
Not sure if this will help,
不确定这是否会有所帮助,
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>
回答by Ganesh Kumbhar
You can use also
你也可以使用
var popup = window.open('......');
popup.onload = function () {
popup.document.title = "my title";
}
回答by Karibasappa G C
Try this, it will work.
试试这个,它会起作用。
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);
}
}