Skip to content

Instantly share code, notes, and snippets.

@mrabbani
Last active February 25, 2019 17:17
Show Gist options
  • Save mrabbani/182d7ab02b9e66150e12118114b4c848 to your computer and use it in GitHub Desktop.
Save mrabbani/182d7ab02b9e66150e12118114b4c848 to your computer and use it in GitHub Desktop.

Open a new page in a new window, print it and closed it stackoverflow

<script type='text/javascript'>
  $(function(){
    function openPrintWindow(url, name, specs) {
      var printWindow = window.open(url, name, specs);
      var printAndClose = function () {
        if (printWindow.document.readyState == 'complete') {
          clearInterval(sched);
          printWindow.print();
          printWindow.close();
        }
      }  
      var sched = setInterval(printAndClose, 200);
    };
    jQuery(document).ready(function ($) {
      $(".test").on("click", function (e) {
        var url =  $(this).attr('data-url'); //Your url

        e.preventDefault();
        openPrintWindow(url, "to_print", "width=700,height=400,_blank");
      });
    });

  });
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment