Created
August 15, 2018 13:14
-
-
Save josephdburdick/e0f522b6187abf7bce554f6b81fc4d48 to your computer and use it in GitHub Desktop.
Print lazy loaded images before printing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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