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?
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?
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>
setTimeout
like this: setTimeout(() => w.document.title = 'This is a test',0);
–
Vaal 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.
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>
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.
If the new window has a file (PDF for example) as an url, it’s possible that the page has no "head" tag.
You have to add one before to modify / add the title.
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 :
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 :
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.
To change title of pdf in newly opened window
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>
Below code works to me on Mozilla Firefox, IE 11 and 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";
const wnd = window.open(url, '_blank');
wnd.onload = function() {
wnd.document.title = 'YOUR TITLE';
}
The only way it worked in my case was using setTimeout like this:
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
}
}
var myWindow = window.open('', '', 'width=600,height=400');
setTimeout(function(){ myWindow.document.title = 'my new title'; }, 1000);
works chrome 2018
You can try writing the html doc: setting the pdf url to an iframe and letting the title tag define the page title.
var w = window.open();
w.document.write(`<html>
<head>
<title>${title}</title>
</head>
<body style="margin: 0; padding: 0">
<iframe src="${pdfInlineUrl}" style="width: 100%; height: 100%; margin: 0; padding: 0; border: none;"></iframe>
</body>
</html>`);
const myWindow = window.open('<myfile>.pdf', 'my-window');
if (myWindow ) {
const title = myWindow.document.createElement('title');
title.innerText = 'my window title';
myWindow.document.head.append(title);
}
You can do it by this
function printData(id) {
console.log(id); //belongs to Div ID
var divToPrint = document.getElementById(id);
var newWin = window.open('');
newWin.document.write('<title>Invoice</title>');
newWin.document.title = id;
newWin.document.write(divToPrint.outerHTML );
newWin.print();
newWin.close();
}
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" integrity="sha384-T3c6CoIi6uLrA9TneNEoa7RxnatzjcDSCmG1MXxSR1GAsXEV/Dwwykc2MPK8M2HN" crossorigin="anonymous">
<button onclick="printData('question-header')" class="btn btn-light text-capitalize border-0" data-mdb-ripple-color="dark"><i class="fas fa-print text-primary"></i> Print</button>
© 2022 - 2024 — McMap. All rights reserved.
window.open()
is the new window's name, not its title. The actual title comes from the<title>
element of the HTML document loaded into that window. Since you're loading a PDF file, it will be left to the discretion of your browser. – Noyes