Last active
December 19, 2015 10:39
-
-
Save luckyjajj/5941738 to your computer and use it in GitHub Desktop.
JavaScript snippet to replace all jpeg images with webp images to reduce network bandwidth.
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
window.onload = function() { | |
var supportsWebP = Modernizr.webp; | |
var allImages = document.body.getElementsByTagName("img"); | |
var length = allImages.length; | |
for(i = 0; i < length; i++){ | |
var image = allImages[i]; | |
var imgSource = image.getAttribute('data-img-source'); | |
image.src = supportsWebP ? imgSource.replace('.jpg', '.webp') : imgSource; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment