lozad.js icon indicating copy to clipboard operation
lozad.js copied to clipboard

On error load fallback

Open marcrobledo opened this issue 7 years ago • 5 comments

It would be great if we could set an image used as a fallback if data-src failed to load.

Example: <img data-src="image.png" data-errorsrc="notfound.png" class="lozad" />

marcrobledo avatar Jul 19 '18 08:07 marcrobledo

@marcrobledo this seems like a good idea, this should be done in conjunction to https://github.com/ApoorvSaxena/lozad.js/pull/79

ApoorvSaxena avatar Jul 19 '18 19:07 ApoorvSaxena

I was just wondering if this has been implemented yet? If not, is the best workaround for now: Load the image via: var tester=new Image(); tester.onerror=imageNotFound; and in the imageNotFound() function, give the data-src a different path to the file we know failed? Seems abit silly as we may end up loading the image twice.

Thanks.

rezelute avatar Nov 14 '18 14:11 rezelute

Can any body let me know fallback is implemented in library or not?

SEIjaz avatar Apr 17 '19 13:04 SEIjaz

Just a workaround:

var observer = lozad('.lozad', {
	loaded: function(el) {
	el.classList.add('loaded');
		try {
			var img = new Image();
			img.src = el.getAttribute('data-src');
			img.addEventListener('error', function(e){
				el.classList.add('error');
			}, false);
		}catch(e){
			console.log(e);
		}
	}
});
observer.observe();

jariesdev avatar Jun 15 '19 05:06 jariesdev

My workaround is:

lozad('.lozad', {
	load: function(el) {
		e.onerror = function() {
			e.src = el.getAttribute('data-errorsrc');
		};
		el.src = el.getAttribute('data-src');
	}
});

vpckso avatar Mar 29 '23 07:03 vpckso