Print Popup in JavaScript

  1. var contents = $("#orderdetail").html(); // get the content to print     
  2. var frame1 = $('<iframe />');  
  3. frame1[0].name = "frame1";  
  4. $("body").append(frame1);  
  5. var framePrint = frame1[0].contentWindow ? frame1[0].contentWindow : frame1[0].contentDocument.document ? frame1[0].contentDocument.document : frame1[0].contentDocument;  
  6. framePrint.document.open();  
  7.   
  8. //Create a new HTML document.    
  9.   
  10. framePrint.document.write('<html><head><title>DIV Contents</title>');  
  11. framePrint.document.write('<link href="assets/bootstrap/css/bootstrap.css" rel="stylesheet" type="text/css"  />');  
  12. framePrint.document.write('<link href="assets/css/print.css" rel="stylesheet" media="print" type="text/css" />'); // class and styles used in content to print    
  13. framePrint.document.write('</head><body>');  
  14.   
  15. //Append the external CSS file.    
  16. //Append the DIV contents.    
  17.   
  18. framePrint.document.write(contents);  
  19. framePrint.document.write('</body></html>');  
  20. framePrint.document.close();  
  21. setTimeout(function()  
  22. {  
  23.     window.frames["frame1"].focus();  
  24.     window.frames["frame1"].print();  
  25.     frame1.remove();  
  26. }, 500);