1
Guys I put together a code in jQuery to make some changes to my layout by hiding some div and printing the page. It works very well, but I’m having the following problem.
When I have image on my page, the system prints before loading the images. How do I print it only after full page loading.
Follows my code:
<script>
// Imprime a página
window.print();
$(document).ready(function () {
var beforePrint = function () {
// Remove o loading
$(".se-pre-con").hide();
// Remove Menu topo
$(".header").hide();
$('body').css({"padding-top": "0px"});
};
var afterPrint = function () {
// Redireciona página
window.location = 'www.google.com';
};
if (window.matchMedia) {
var mediaQueryList = window.matchMedia('print');
mediaQueryList.addListener(function (mql) {
if (mql.matches) {
beforePrint();
} else {
afterPrint();
}
});
}
window.onbeforeprint = beforePrint;
window.onafterprint = afterPrint;
}());
</script>
_______________________________ I’m trying like this ______________________
<script>
var root = this;
$(document).ready(function () {
var beforePrint = function () {
// Remove o loading
$(".se-pre-con").hide();
// Remove Menu topo
$(".header").hide();
$('body').css({"padding-top": "0px"});
};
var afterPrint = function () {
// Redireciona página
window.location = 'google.com';
};
if (window.matchMedia) {
var mediaQueryList = window.matchMedia('print');
mediaQueryList.addListener(function (mql) {
if (mql.matches) {
beforePrint();
} else {
afterPrint();
}
});
}
window.onbeforeprint = beforePrint;
window.onafterprint = afterPrint;
/* Imprime a página */
root['print']();
});
</script>
Well I tried to do the way you explained here, and it’s not working, could you give me an example with my code? I broke my head here and not right, it prints but still does not load the images
– Hugo Borges
I’m not loading it into html anyway. I’ll edit my question so you can see I’m trying ok
– Hugo Borges
Look I switched the
root['print']();forsetTimeout(root.print, 1);the only problem is that it takes 1 second to send the print command.– Hugo Borges
good the
requestAnimationFrame(root.print)didn’t work, just doesn’t print. well I guess thesetTimeout(root.print, 300)is not functional for those who have slow internet, imagine if it goes past 300ms and the image has not yet uploaded. Will give problem right.– Hugo Borges
I updated the answer. And, no, this does not depend on the internet, the images will already have loaded before the callback of the "ready" event run.
– Klaider