Am trying to create a simple lazy image load to replace default image with main item image after page has loaded and image is fully downloaded. But am getting and error using below jquery code.
HTML Example
<img src="default.png" data-src="image-1.png" class="productImage"/>
<img src="default.png" data-src="image-2.png" class="productImage"/>
<img src="default.png" data-src="image-3.png" class="productImage"/>
<img src="default.png" data-src="image-4.png" class="productImage"/>
Jquery Example
$(function(){
$('img.productImage').each(function(){
var this_image = this;
var src = $(this_image).attr('src');
var lsrc = $(this_image).attr('data-src');
if(lsrc.length > 0){
var img = new Image();
img.src = lsrc;
$(img).load(function() {
this_image.src = this.src;
});
}else{
this_image.src = src;
}
});
});
The above script give this error TypeError: e.indexOf is not a function[Learn More] jquery-3.2.1.min.js:2:82466
.
I have also tried to use below code and it worked but am not very sure that it does wait for the image to download before replacing the src, because it always replace one page load.
$(function(){
$('img.productImage').each(function(){
var this_image = this;
var src = $(this_image).attr('src');
var lsrc = $(this_image).attr('data-src');
if(lsrc.length > 0){
this_image.src = lsrc;
}else{
this_image.src = src;
}
});
});