jQuery printElement not showing images in Chrome (FIX)

hello,

for you who are facing this issue when using jQuery printElement plugin with chrome, here is the fix

this issue is happening only in chrome but in firefox or IE it's working properly,

so what is the cause of this problem:

if you look at this line in your "jquery.printElement.js" file:

html.push('<script type="text/javascript">function printPage(){focus();print();' + ((!$.browser.opera && !opts["leaveOpen"] && opts["printMode"].toLowerCase() == 'popup') ? 'close();' : '') + '}</script>');

you will see that it's not waiting for the document to be loaded in order to start printing, rather, it's printing the page directly, since there is some delay in loading images in chrome, you have to wait for the page to be fully loaded


so that solution would be:

you have to replace that line with this one:

html.push('<script type="text/javascript">function printPage(){focus();$(document).ready(function(){print();});' + ((!$.browser.opera && !opts["leaveOpen"] && opts["printMode"].toLowerCase() == 'popup') ? 'close();' : '') + '}</script>');

this one waits for the page to be fully loaded then prints the page, BUT, since you don't have the jQuery main js file, you have to put it like this:

html.push('<script type="text/javascript" src="jquery.js"></script>');

so here is the final result:

replace this:
html.push('<script type="text/javascript">function printPage(){focus();print();' + ((!$.browser.opera && !opts["leaveOpen"] && opts["printMode"].toLowerCase() == 'popup') ? 'close();' : '') + '}</script>');

with those:
html.push('<script type="text/javascript" src="jquery.js"></script>');

html.push('<script type="text/javascript">function printPage(){focus();$(document).ready(function(){print();});' + ((!$.browser.opera && !opts["leaveOpen"] && opts["printMode"].toLowerCase() == 'popup') ? 'close();' : '') + '}</script>');

happy coding :)

No Comments