Javascript 如何使用 window.open() 显示窗口标题?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8051811/
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
how to show window title using window.open()?
提问by user811433
I want to open a new window using:
我想使用以下方法打开一个新窗口:
window.open('<myfile>.pdf','my window','resizable,scrollbars');
The new window opens, but I do not get the title of the window as 'my window'. What could be going wrong?
新窗口打开,但我没有得到窗口标题为“我的窗口”。可能出什么问题了?
回答by Muhammad Shoaib
If domain is same then you can change the title of new window
如果域相同,则可以更改新窗口的标题
<script type="text/javascript">
var w = window.open('http://localhost:4885/UMS2/Default.aspx');
w.document.title = 'testing';
</script>
回答by maniootek
Here is my solution, please check this out:
这是我的解决方案,请查看:
var myWindow = window.open('<myfile>.pdf','my window','resizable,scrollbars');
myWindow.document.write('<title>My PDF File Title</title>');
Hope I could help.
希望我能帮上忙。
回答by Tony M
This is what I did:
这就是我所做的:
<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>
in HTML body <U><A style="cursor: pointer;" onclick="OpenWindow()">Open in New Window</a></U>
在 HTML 正文中 <U><A style="cursor: pointer;" onclick="OpenWindow()">Open in New Window</a></U>
回答by Louis Perrin
If the new window has a file (PDF for example) as an url, it's possible that the page has no "head" tag.
如果新窗口有一个文件(例如 PDF)作为 url,则该页面可能没有“head”标签。
You have to add one before to modify / add the title.
您必须先添加一个才能修改/添加标题。
jQuery :
jQuery :
var w = window.open('/path/to/your/file.pdf');// or any url
$(w.document).find('html').append('<head><title>your title</title></head>');
Native js :
原生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'));
Now if the page is long to load, you may add a onload watch, then also a timeout. In my case, I had to code like that :
现在,如果页面加载时间过长,您可以添加一个 onload 监视,然后还有一个超时。就我而言,我必须这样编码:
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.
回答by Blazemonger
The JavaScript "title" argument is a variable to be used inside of JavaScript. The actual title written in the top of the window normally comes from the HTML <title>
tag, but you don't have that since you're showing a PDF.
JavaScript“title”参数是一个在 JavaScript 中使用的变量。写在窗口顶部的实际标题通常来自 HTML<title>
标记,但由于您显示的是 PDF,因此您没有该标记。
回答by Siva Ganesh
To change title of pdf in newly opened window
在新打开的窗口中更改 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>
回答by sameer Memon
Below code works to me on Mozilla Firefox, IE 11 and Google Chrome.
下面的代码适用于 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";
回答by Leniel Maccaferri
The only way it worked in my case was using setTimeoutlike this:
它在我的情况下工作的唯一方法是使用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
}
}
回答by waza123
var myWindow = window.open('', '', 'width=600,height=400');
setTimeout(function(){ myWindow.document.title = 'my new title'; }, 1000);
works chrome 2018
作品 chrome 2018
回答by Ankit Pundhir
You can try to capture window.onload
event for that show as below:
您可以尝试window.onload
为该节目捕获事件,如下所示:
var w = window.open('https://your_website_url');
var title = 'Your Custom Title';
w.onload = function(){
w.document.title = title;
};