Skip to content

Instantly share code, notes, and snippets.

@luckyjajj
Last active December 19, 2015 10:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save luckyjajj/5941738 to your computer and use it in GitHub Desktop.
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.
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