3

This is my try, were it opened on newtab, But its always showing test pdf as title

function titlepath(path,name){
    alert(path);
    alert(name);
    document.title = name;
    window.open(path, '_blank');
}
CBroe
  • 91,630
  • 14
  • 92
  • 150
Siva Ganesh
  • 1,415
  • 2
  • 16
  • 28
  • 1
    You are trying to set the title of the document this JS code is in here, you are not even accessing the popup window … – CBroe Jun 08 '18 at 11:41
  • `document.title` refers to the window you're in _now_, not the one you're opening, Even then I'm not sure it'll work, since a PDF is not a HTML document, so you can't set its title. Javascript can only affect HTML documents. The browser is just displaying a file, so it'll use the name of the file – ADyson Jun 08 '18 at 11:42
  • You would have to use an HTML document, and display the PDF in an ` –  Jun 08 '18 at 11:42
  • Sorry sir, Then how to do sir? – Siva Ganesh Jun 08 '18 at 11:42
  • @ChrisG But I dont want to do in – Siva Ganesh Jun 08 '18 at 11:43
  • 2
    Read my edited comment above, I don't think this is possible. You're outside the realms of a HTML document when you do this, so you can't use JS to affect what happens. It's just part of the browser's functionality, it's not part of your web page anymore. – ADyson Jun 08 '18 at 11:44
  • @ADyson Any chance to do it sir, Except – Siva Ganesh Jun 08 '18 at 11:45
  • Like I said twice already, I don't think so. However, I searched a little, I found this: https://stackoverflow.com/a/37299858/5947043 but I don't know if it actually works or not, you can try it, or search some more (I googled for "window.open set title") – ADyson Jun 08 '18 at 11:47
  • Thanks and let me check – Siva Ganesh Jun 08 '18 at 11:47
  • Just saw, you asked this yesterday already - https://stackoverflow.com/q/50737670/1427878 Please stop creating multiple questions about the same issue. – CBroe Jun 08 '18 at 12:44
  • sure sir, But yesterday I tried in php, Now I tried in jquery – Siva Ganesh Jun 08 '18 at 12:45
  • 1
    Does this answer your question? [How to set title name of the pdf. While viewing the Document(New Tab)](https://stackoverflow.com/questions/50737670/how-to-set-title-name-of-the-pdf-while-viewing-the-documentnew-tab) – Prashant Pokhriyal Nov 09 '22 at 07:20

1 Answers1

4

This solution works for me. Question: How to change the title of pdf in newly opened tab

     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();
        }
Siva Ganesh
  • 1,415
  • 2
  • 16
  • 28