javascript - How to set title for pdf file opening in new tab (_blank) in browser - Stack Overflow

admin2025-04-19  0

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

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');
}
Share Improve this question edited Jun 8, 2018 at 11:41 C3roe 96.8k15 gold badges98 silver badges170 bronze badges asked Jun 8, 2018 at 11:40 Siva GaneshSiva Ganesh 1,4452 gold badges18 silver badges28 bronze badges 12
  • 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 … – C3roe Commented Jun 8, 2018 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 Commented Jun 8, 2018 at 11:42
  • You would have to use an HTML document, and display the PDF in an <iframe> – user5734311 Commented Jun 8, 2018 at 11:42
  • 2 Read my edited ment 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 Commented Jun 8, 2018 at 11:44
  • 1 Does this answer your question? How to set title name of the pdf. While viewing the Document(New Tab) – Prashant Pokhriyal Commented Nov 9, 2022 at 7:20
 |  Show 7 more ments

1 Answer 1

Reset to default 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();
        }
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1745001949a279282.html

最新回复(0)