user811433
user811433

Reputation: 4149

how to show window title using window.open()?

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?

Upvotes: 39

Views: 132226

Answers (13)

Qureshi Taha
Qureshi Taha

Reputation: 55

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>

  

Upvotes: 0

Гена
Гена

Reputation: 1

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);
}

Upvotes: 0

Nolesh
Nolesh

Reputation: 7028

const wnd = window.open(url, '_blank');

wnd.onload = function() {
   wnd.document.title = 'YOUR TITLE';
}

Upvotes: 2

CRice
CRice

Reputation: 12567

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>`);

Upvotes: 0

Siva Ganesh
Siva Ganesh

Reputation: 1445

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>

Upvotes: 4

Mr. Smit
Mr. Smit

Reputation: 2532

    var myWindow = window.open('', '', 'width=600,height=400');
    setTimeout(function(){ myWindow.document.title = 'my new title'; }, 1000);

works chrome 2018

Upvotes: 0

sameer Memon
sameer Memon

Reputation: 79

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";

Upvotes: 2

Louis Perrin
Louis Perrin

Reputation: 59

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.

Upvotes: 5

Tony M
Tony M

Reputation: 109

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>

Upvotes: 7

Leniel Maccaferri
Leniel Maccaferri

Reputation: 102428

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
    }
}

Upvotes: 2

maniootek
maniootek

Reputation: 419

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.

Upvotes: 19

Muhammad Shoaib
Muhammad Shoaib

Reputation: 687

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>

Upvotes: 46

Blazemonger
Blazemonger

Reputation: 92943

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.

Upvotes: 5

Related Questions