Suggest a feature
×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: Javascript
Posted by: Amit Sarker
Added: Feb 27, 2019 12:12 PM
Views: 45
Tags: no tags
  1. function PrintDivContentByDivId(divId) {
  2.     var contents = document.getElementById(divId).innerHTML;
  3.     var frame1 = document.createElement('iframe');
  4.     frame1.name = "frame1";
  5.     frame1.style.position = "absolute";
  6.     frame1.style.top = "-1000000px";
  7.     document.body.appendChild(frame1);
  8.     var frameDoc = frame1.contentWindow ? frame1.contentWindow : frame1.contentDocument.document ? frame1.contentDocument.document : frame1.contentDocument;
  9.     frameDoc.document.open();
  10.     frameDoc.document.write('<html><head><title>DIV Contents</title>');
  11.     frameDoc.document.write('</head><body>');
  12.     frameDoc.document.write(contents);
  13.     frameDoc.document.write('</body></html>');
  14.     frameDoc.document.close();
  15.     setTimeout(function () {
  16.         window.frames["frame1"].focus();
  17.         window.frames["frame1"].print();
  18.         document.body.removeChild(frame1);
  19.     }, 500);
  20.     return false;
  21. }