Print the Content of the Window on Focus

  1. var printContents = document.getElementById("bodyID").innerHTML;    
  2.     var originalContents = document.body.innerHTML;    
  3.     document.body.innerHTML = printContents;    
  4.     var mywindow = window.open('''my div''height=400,width=600');    
  5.     mywindow.document.write('<html><head><title></title>');    
  6.     /*optional stylesheet*/ //mywindow.document.write('<link rel="stylesheet" href="main.css" type="text/css" />');    
  7.     mywindow.document.write('</head><body >');    
  8.     mywindow.document.write(printContents);    
  9.     mywindow.document.write('</body></html>');    
  10.     mywindow.document.close(); // necessary for IE >= 10    
  11.     mywindow.focus(); // necessary for IE >= 10    
  12.     mywindow.print();    
  13.     mywindow.close();    
  14.     document.body.innerHTML = originalContents;    
  15.     window.location.href = "/Home/Index"  
Suppose you are on the Home page, you want some other page data to be printed, then navigate on click of the print icon to the print page and then Add a JS file onto that Print page with the above script code.

This will surely help ..:).