Open print dialog window with different HTML content





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







7















In order to open a print dialog for the current page we do something like so:



<a href="javascript:window.print()">Print</a>


How to make a link in the current page that will open the print dialog with different context than the actual page?





Print dialog box in Chrome:
enter image description here










share|improve this question































    7















    In order to open a print dialog for the current page we do something like so:



    <a href="javascript:window.print()">Print</a>


    How to make a link in the current page that will open the print dialog with different context than the actual page?





    Print dialog box in Chrome:
    enter image description here










    share|improve this question



























      7












      7








      7


      1






      In order to open a print dialog for the current page we do something like so:



      <a href="javascript:window.print()">Print</a>


      How to make a link in the current page that will open the print dialog with different context than the actual page?





      Print dialog box in Chrome:
      enter image description here










      share|improve this question
















      In order to open a print dialog for the current page we do something like so:



      <a href="javascript:window.print()">Print</a>


      How to make a link in the current page that will open the print dialog with different context than the actual page?





      Print dialog box in Chrome:
      enter image description here







      javascript html css printing






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 17 '18 at 1:31







      Lior Elrom

















      asked Nov 6 '14 at 18:10









      Lior ElromLior Elrom

      8,080115269




      8,080115269
























          4 Answers
          4






          active

          oldest

          votes


















          8














          I'm not sure if this is actually an answer to your question since your question has very little detail about what you mean by opening a new window.



          If you are on /page/one and you want to have a link open /report/page and automatically bring up the print dialog...then that is as easy as triggering window.print on the load event, e.g.



          <body onload='window.print()'>


          If you don't want that page to always open the print dialog, then make the link to the new page /report/page?print=1 or something to that effect when you want to print, and when you find that in the URL trigger the onload event.






          share|improve this answer































            7














            Print Dialog



            After playing around (and some googling), I came out with this solution:



            I added a non-printable class to the current view and printable class to the printable container element. In my CSS, I used css media queries where @media screen and @media print states contains the corresponding display behavior:



            @media screen {
            .printable { display: none; }
            .non-printable { display: block; }
            }
            @media print {
            .printable { display: block; }
            .non-printable { display: none; }
            }


            Now, I can print new content from my current view without opening a new tab or changing my current view.



            Check out this jsFiddle and the supported browser's list.






            share|improve this answer

































              1














              There's a nice Javascript plugin called PrintThis that accomplishes this very easily.



              You just need to use a jQuery selector and call the plugin, e.g.:



              $('selector').printThis();


              https://github.com/jasonday/printThis






              share|improve this answer































                0














                Include printable content in a div. Like:



                HTML:



                <div id='printarea'>
                <p>This is a sample text for printing purpose.</p>
                <input type='button' id='btn' value='Print' onclick='printFunc();'>
                </div>
                <p>Do not print.</p>


                JQuery:



                function printFunc() {
                var divToPrint = document.getElementById('printarea');
                var htmlToPrint = '' +
                '<style type="text/css">' +
                'table th, table td {' +
                'border:1px solid #000;' +
                'padding;0.5em;' +
                '}' +
                '</style>';
                htmlToPrint += divToPrint.outerHTML;
                newWin = window.open("");
                newWin.document.write("<h3 align='center'>Print Page</h3>");
                newWin.document.write(htmlToPrint);
                newWin.print();
                newWin.close();
                }


                It will just print the printable div.Thanks






                share|improve this answer
























                  Your Answer






                  StackExchange.ifUsing("editor", function () {
                  StackExchange.using("externalEditor", function () {
                  StackExchange.using("snippets", function () {
                  StackExchange.snippets.init();
                  });
                  });
                  }, "code-snippets");

                  StackExchange.ready(function() {
                  var channelOptions = {
                  tags: "".split(" "),
                  id: "1"
                  };
                  initTagRenderer("".split(" "), "".split(" "), channelOptions);

                  StackExchange.using("externalEditor", function() {
                  // Have to fire editor after snippets, if snippets enabled
                  if (StackExchange.settings.snippets.snippetsEnabled) {
                  StackExchange.using("snippets", function() {
                  createEditor();
                  });
                  }
                  else {
                  createEditor();
                  }
                  });

                  function createEditor() {
                  StackExchange.prepareEditor({
                  heartbeatType: 'answer',
                  autoActivateHeartbeat: false,
                  convertImagesToLinks: true,
                  noModals: true,
                  showLowRepImageUploadWarning: true,
                  reputationToPostImages: 10,
                  bindNavPrevention: true,
                  postfix: "",
                  imageUploader: {
                  brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
                  contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
                  allowUrls: true
                  },
                  onDemand: true,
                  discardSelector: ".discard-answer"
                  ,immediatelyShowMarkdownHelp:true
                  });


                  }
                  });














                  draft saved

                  draft discarded


















                  StackExchange.ready(
                  function () {
                  StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f26786460%2fopen-print-dialog-window-with-different-html-content%23new-answer', 'question_page');
                  }
                  );

                  Post as a guest















                  Required, but never shown

























                  4 Answers
                  4






                  active

                  oldest

                  votes








                  4 Answers
                  4






                  active

                  oldest

                  votes









                  active

                  oldest

                  votes






                  active

                  oldest

                  votes









                  8














                  I'm not sure if this is actually an answer to your question since your question has very little detail about what you mean by opening a new window.



                  If you are on /page/one and you want to have a link open /report/page and automatically bring up the print dialog...then that is as easy as triggering window.print on the load event, e.g.



                  <body onload='window.print()'>


                  If you don't want that page to always open the print dialog, then make the link to the new page /report/page?print=1 or something to that effect when you want to print, and when you find that in the URL trigger the onload event.






                  share|improve this answer




























                    8














                    I'm not sure if this is actually an answer to your question since your question has very little detail about what you mean by opening a new window.



                    If you are on /page/one and you want to have a link open /report/page and automatically bring up the print dialog...then that is as easy as triggering window.print on the load event, e.g.



                    <body onload='window.print()'>


                    If you don't want that page to always open the print dialog, then make the link to the new page /report/page?print=1 or something to that effect when you want to print, and when you find that in the URL trigger the onload event.






                    share|improve this answer


























                      8












                      8








                      8







                      I'm not sure if this is actually an answer to your question since your question has very little detail about what you mean by opening a new window.



                      If you are on /page/one and you want to have a link open /report/page and automatically bring up the print dialog...then that is as easy as triggering window.print on the load event, e.g.



                      <body onload='window.print()'>


                      If you don't want that page to always open the print dialog, then make the link to the new page /report/page?print=1 or something to that effect when you want to print, and when you find that in the URL trigger the onload event.






                      share|improve this answer













                      I'm not sure if this is actually an answer to your question since your question has very little detail about what you mean by opening a new window.



                      If you are on /page/one and you want to have a link open /report/page and automatically bring up the print dialog...then that is as easy as triggering window.print on the load event, e.g.



                      <body onload='window.print()'>


                      If you don't want that page to always open the print dialog, then make the link to the new page /report/page?print=1 or something to that effect when you want to print, and when you find that in the URL trigger the onload event.







                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Nov 6 '14 at 18:20









                      Kevin NelsonKevin Nelson

                      6,33232236




                      6,33232236

























                          7














                          Print Dialog



                          After playing around (and some googling), I came out with this solution:



                          I added a non-printable class to the current view and printable class to the printable container element. In my CSS, I used css media queries where @media screen and @media print states contains the corresponding display behavior:



                          @media screen {
                          .printable { display: none; }
                          .non-printable { display: block; }
                          }
                          @media print {
                          .printable { display: block; }
                          .non-printable { display: none; }
                          }


                          Now, I can print new content from my current view without opening a new tab or changing my current view.



                          Check out this jsFiddle and the supported browser's list.






                          share|improve this answer






























                            7














                            Print Dialog



                            After playing around (and some googling), I came out with this solution:



                            I added a non-printable class to the current view and printable class to the printable container element. In my CSS, I used css media queries where @media screen and @media print states contains the corresponding display behavior:



                            @media screen {
                            .printable { display: none; }
                            .non-printable { display: block; }
                            }
                            @media print {
                            .printable { display: block; }
                            .non-printable { display: none; }
                            }


                            Now, I can print new content from my current view without opening a new tab or changing my current view.



                            Check out this jsFiddle and the supported browser's list.






                            share|improve this answer




























                              7












                              7








                              7







                              Print Dialog



                              After playing around (and some googling), I came out with this solution:



                              I added a non-printable class to the current view and printable class to the printable container element. In my CSS, I used css media queries where @media screen and @media print states contains the corresponding display behavior:



                              @media screen {
                              .printable { display: none; }
                              .non-printable { display: block; }
                              }
                              @media print {
                              .printable { display: block; }
                              .non-printable { display: none; }
                              }


                              Now, I can print new content from my current view without opening a new tab or changing my current view.



                              Check out this jsFiddle and the supported browser's list.






                              share|improve this answer















                              Print Dialog



                              After playing around (and some googling), I came out with this solution:



                              I added a non-printable class to the current view and printable class to the printable container element. In my CSS, I used css media queries where @media screen and @media print states contains the corresponding display behavior:



                              @media screen {
                              .printable { display: none; }
                              .non-printable { display: block; }
                              }
                              @media print {
                              .printable { display: block; }
                              .non-printable { display: none; }
                              }


                              Now, I can print new content from my current view without opening a new tab or changing my current view.



                              Check out this jsFiddle and the supported browser's list.







                              share|improve this answer














                              share|improve this answer



                              share|improve this answer








                              edited Feb 28 '16 at 6:05

























                              answered Nov 6 '14 at 20:24









                              Lior ElromLior Elrom

                              8,080115269




                              8,080115269























                                  1














                                  There's a nice Javascript plugin called PrintThis that accomplishes this very easily.



                                  You just need to use a jQuery selector and call the plugin, e.g.:



                                  $('selector').printThis();


                                  https://github.com/jasonday/printThis






                                  share|improve this answer




























                                    1














                                    There's a nice Javascript plugin called PrintThis that accomplishes this very easily.



                                    You just need to use a jQuery selector and call the plugin, e.g.:



                                    $('selector').printThis();


                                    https://github.com/jasonday/printThis






                                    share|improve this answer


























                                      1












                                      1








                                      1







                                      There's a nice Javascript plugin called PrintThis that accomplishes this very easily.



                                      You just need to use a jQuery selector and call the plugin, e.g.:



                                      $('selector').printThis();


                                      https://github.com/jasonday/printThis






                                      share|improve this answer













                                      There's a nice Javascript plugin called PrintThis that accomplishes this very easily.



                                      You just need to use a jQuery selector and call the plugin, e.g.:



                                      $('selector').printThis();


                                      https://github.com/jasonday/printThis







                                      share|improve this answer












                                      share|improve this answer



                                      share|improve this answer










                                      answered Aug 7 '17 at 23:45









                                      Alejandro Pablo TkachukAlejandro Pablo Tkachuk

                                      1,4451314




                                      1,4451314























                                          0














                                          Include printable content in a div. Like:



                                          HTML:



                                          <div id='printarea'>
                                          <p>This is a sample text for printing purpose.</p>
                                          <input type='button' id='btn' value='Print' onclick='printFunc();'>
                                          </div>
                                          <p>Do not print.</p>


                                          JQuery:



                                          function printFunc() {
                                          var divToPrint = document.getElementById('printarea');
                                          var htmlToPrint = '' +
                                          '<style type="text/css">' +
                                          'table th, table td {' +
                                          'border:1px solid #000;' +
                                          'padding;0.5em;' +
                                          '}' +
                                          '</style>';
                                          htmlToPrint += divToPrint.outerHTML;
                                          newWin = window.open("");
                                          newWin.document.write("<h3 align='center'>Print Page</h3>");
                                          newWin.document.write(htmlToPrint);
                                          newWin.print();
                                          newWin.close();
                                          }


                                          It will just print the printable div.Thanks






                                          share|improve this answer




























                                            0














                                            Include printable content in a div. Like:



                                            HTML:



                                            <div id='printarea'>
                                            <p>This is a sample text for printing purpose.</p>
                                            <input type='button' id='btn' value='Print' onclick='printFunc();'>
                                            </div>
                                            <p>Do not print.</p>


                                            JQuery:



                                            function printFunc() {
                                            var divToPrint = document.getElementById('printarea');
                                            var htmlToPrint = '' +
                                            '<style type="text/css">' +
                                            'table th, table td {' +
                                            'border:1px solid #000;' +
                                            'padding;0.5em;' +
                                            '}' +
                                            '</style>';
                                            htmlToPrint += divToPrint.outerHTML;
                                            newWin = window.open("");
                                            newWin.document.write("<h3 align='center'>Print Page</h3>");
                                            newWin.document.write(htmlToPrint);
                                            newWin.print();
                                            newWin.close();
                                            }


                                            It will just print the printable div.Thanks






                                            share|improve this answer


























                                              0












                                              0








                                              0







                                              Include printable content in a div. Like:



                                              HTML:



                                              <div id='printarea'>
                                              <p>This is a sample text for printing purpose.</p>
                                              <input type='button' id='btn' value='Print' onclick='printFunc();'>
                                              </div>
                                              <p>Do not print.</p>


                                              JQuery:



                                              function printFunc() {
                                              var divToPrint = document.getElementById('printarea');
                                              var htmlToPrint = '' +
                                              '<style type="text/css">' +
                                              'table th, table td {' +
                                              'border:1px solid #000;' +
                                              'padding;0.5em;' +
                                              '}' +
                                              '</style>';
                                              htmlToPrint += divToPrint.outerHTML;
                                              newWin = window.open("");
                                              newWin.document.write("<h3 align='center'>Print Page</h3>");
                                              newWin.document.write(htmlToPrint);
                                              newWin.print();
                                              newWin.close();
                                              }


                                              It will just print the printable div.Thanks






                                              share|improve this answer













                                              Include printable content in a div. Like:



                                              HTML:



                                              <div id='printarea'>
                                              <p>This is a sample text for printing purpose.</p>
                                              <input type='button' id='btn' value='Print' onclick='printFunc();'>
                                              </div>
                                              <p>Do not print.</p>


                                              JQuery:



                                              function printFunc() {
                                              var divToPrint = document.getElementById('printarea');
                                              var htmlToPrint = '' +
                                              '<style type="text/css">' +
                                              'table th, table td {' +
                                              'border:1px solid #000;' +
                                              'padding;0.5em;' +
                                              '}' +
                                              '</style>';
                                              htmlToPrint += divToPrint.outerHTML;
                                              newWin = window.open("");
                                              newWin.document.write("<h3 align='center'>Print Page</h3>");
                                              newWin.document.write(htmlToPrint);
                                              newWin.print();
                                              newWin.close();
                                              }


                                              It will just print the printable div.Thanks







                                              share|improve this answer












                                              share|improve this answer



                                              share|improve this answer










                                              answered Nov 9 '17 at 15:42









                                              BibanCSBibanCS

                                              797




                                              797






























                                                  draft saved

                                                  draft discarded




















































                                                  Thanks for contributing an answer to Stack Overflow!


                                                  • Please be sure to answer the question. Provide details and share your research!

                                                  But avoid



                                                  • Asking for help, clarification, or responding to other answers.

                                                  • Making statements based on opinion; back them up with references or personal experience.


                                                  To learn more, see our tips on writing great answers.




                                                  draft saved


                                                  draft discarded














                                                  StackExchange.ready(
                                                  function () {
                                                  StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f26786460%2fopen-print-dialog-window-with-different-html-content%23new-answer', 'question_page');
                                                  }
                                                  );

                                                  Post as a guest















                                                  Required, but never shown





















































                                                  Required, but never shown














                                                  Required, but never shown












                                                  Required, but never shown







                                                  Required, but never shown

































                                                  Required, but never shown














                                                  Required, but never shown












                                                  Required, but never shown







                                                  Required, but never shown







                                                  Popular posts from this blog

                                                  Bressuire

                                                  Vorschmack

                                                  Quarantine