Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save josephdburdick/e0f522b6187abf7bce554f6b81fc4d48 to your computer and use it in GitHub Desktop.
Save josephdburdick/e0f522b6187abf7bce554f6b81fc4d48 to your computer and use it in GitHub Desktop.
Print lazy loaded images before printing
const LoadLazyImagesAndPrint = () => {
// Loop through every lazy loaded element
const imageDivs = [...document.querySelectorAll('.image')]
.map(imageDiv => {
let image = imageDiv.querySelector('img');
// Determine if the image is unloaded
if (!image.src) {
// Load the image element string from the accompanying <noscript> element
const unloadedImageString = imageDiv.querySelector('noscript').innerText;
// Replace the unloaded image
image.outerHTML = unloadedImageString;
// Append class name
image.classList += ' is-loaded';
}
});
setTimeout(() => window.print(), 2000);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment