Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

onerror default image with 2.x #430

Open
onrclk opened this issue Feb 2, 2019 · 2 comments
Open

onerror default image with 2.x #430

onrclk opened this issue Feb 2, 2019 · 2 comments

Comments

@onrclk
Copy link

onrclk commented Feb 2, 2019

Is there any way to load default image on error with 2.x version ? it was possible with 1.x version through setting error listener like following.

on("error", function() {
$self.attr("src","/resources/image/error.jpg");
})

how can i do that using div or img element with 2.x version ?

@brianwozeniak
Copy link

brianwozeniak commented Feb 16, 2019

I am also really needing this feature to load a default image if the other images fail to load, still trying to figure out a solution here.

@brianwozeniak
Copy link

I have updated the code to be able to handle loading a default image on error:

https://github.com/brianwozeniak/jquery_lazyload/blob/2.x/lazyload.js

So for example:

<img src="default.jpg" data-src="imageToLoad.jpg" data-srcset="imageToLoad.jpg 1x, imageToLoadRetina.jpg 2x">

If the image in data-src or data-srcset fail to load, then it will change back to the default src value. It does this by creating a data-original attribute when trying to load the other images.

I also created a pull request to have this merged in. I also re-factored some duplicate code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants