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

寵の児 提交于 2019-12-19 13:48:23

问题


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?


回答1:


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>



回答2:


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.




回答3:


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>




回答4:


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.




回答5:


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.



回答6:


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>



回答7:


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



回答8:


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



回答9:


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

works chrome 2018




回答10:


You can try to capture window.onload event for that show as below:

var w = window.open('https://your_website_url');
var title = 'Your Custom Title';
w.onload = function(){
    w.document.title = title;
};


来源:https://stackoverflow.com/questions/8051811/how-to-show-window-title-using-window-open

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!