Javascript Print function - file name

25,398

Solution 1

onClick="document.title = 'My new title';window.print();"

You can try it here:

    <html>
    <head>
    <title>My title</title>
    </head>
    <body>
    <button onClick="document.title = 'My new title';window.print();">Try it</button>
    <p id="demo"></p>
    </body>
    </html>

Solution 2

According to MDC, window.print() has no parameters to control anything.

https://developer.mozilla.org/en/DOM/window.print

Share:
25,398
Samuel Meddows
Author by

Samuel Meddows

A full time IT student in Brisbane Australia @ QUT. I specialize in web development and networking.

Updated on July 05, 2022

Comments

  • Samuel Meddows
    Samuel Meddows almost 2 years

    I am using the window.print() function in javaScript to print a page which is executed from a button onClick.

    <input type="submit" value="Print" class="register-button" onClick="window.print()"/>
    

    In the popup the default filename is the name of the website found it the tags in the header.

    Is there anyway I can set a parameter in the function so the default save filename is what ever I want it to be?

    Cheers.

  • Samuel Meddows
    Samuel Meddows over 13 years
    Hey LAS_VEGAS. That didn't quite work but put it into a function and called that instead and it worked fine!! Although it does change the name at the top of the page but it's not much of a worry to me. function driverprint(){ document.title = "Sam"; window.print(); }
  • Samuel Meddows
    Samuel Meddows over 13 years
    Cheers SHiNKiROU for looking into. I managed to hack around the issue but will need to look into a different approach.
  • Caner
    Caner over 13 years
    You can try to change back: driverprint(){ document.title = "Sam"; window.print(); document.title = "LAS_VEGAS";}
  • Phil
    Phil almost 5 years
    This does work in Google Chrome but not in Firefox 68.0.1
  • Caner
    Caner almost 5 years
    @Phil I just tested on Firefox and it is working for me
  • Phil
    Phil almost 5 years
    @Caner If i run the code snippet, it does not work for me in Firefox 68.0.1 :( And I tried it in my own implementation without success.
  • Caner
    Caner almost 5 years
    @Phil maybe you can post a question on SO, it works for me on 68.0.2 on mac