JavaScript Frame does not print images. V2018 (Solved) updated 18/03/10

Personally, I like to use javascript and frame for printing, but I came across a problem, my images were not printed.

Finding out that the problem was in the time the image was passed to the iframe, do you know what the solution was? I had to delay the print on v2018.
v2 works without having to delay printing.
So v2018 is a bit slower.

As I like to use the newer version, I will leave the code for the friends who are printing via javascript and iframe.

code:

// Create a jquery plugin that prints the given element.
// Wayos 1.7 (jQuery 3.3.1) 18/03/10
// ------------ Thanks ------------
// > bennadel: https://www.bennadel.com/blog/1591-ask-ben-print-part-of-a-web-page-with-jquery.htm
// > btd: https://gist.github.com/btd/2390721
// > javascript.info:  https://javascript.info/settimeout-setinterval
// Update new vesion by Wayos(DjSync)
jQuery.fn.print = function() {	
"use strict";
// Create unique ID	
var uid;
uid = (new Date().getTime()).toString(36);
	
// Create a random ID for the print frame.
var strFrameName = ("printer-"+uid);

// Create an iFrame with the new name.
var jFrame = jQuery("<iframe name='"+strFrameName+"'>");

// Styles to hide the frame.
jFrame
.css("position","absolute")
.css("visibility","hidden")
.css("width","950px")
.css("height","100%")
.css("top","69px")
.css("left","240px")
.css("background-color","#fff")
.css("z-index","-1")
.appendTo($("body:first"));

// Get a FRAMES reference to the new frame.
var objFrame = window.frames[strFrameName];       

// Get a reference to the DOM in the new frame.
var objDoc = objFrame.document;

// Write out the HTML of the current element.
objDoc.open();
objDoc.write("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">");
objDoc.write("<html>");
objDoc.write("<body>");
objDoc.write("<head>");
objDoc.write("<title>");
objDoc.write(document.title);
objDoc.write("</title>");
objDoc.write("</head>");
objDoc.write(this.html());
objDoc.write("</body>");
objDoc.write("</html>");
objDoc.close();

// Print delay ExeOutput v2018.
var printdelay;
printdelay = setTimeout(function(){
objFrame.focus();
objFrame.print();},100); //(100ms)

// Let's remove the frame and clear the timeout, thus leaving ready for a new impression.
setTimeout(function(){
jFrame.remove();
clearTimeout(printdelay);	
},200); //(200ms)

}

// onclick button to print
var go_print;

   go_print= function(){
   "use strict";
   $('#DivToPrint').print();
   }
2 Likes