Skip to main content

Posts

Showing posts from September, 2015

Js/jQuery load alternative images synchronously?

You can find lot of links over Internet, how to load alternative images but most of them give examples of asynchronously loading not synchronously one. I was facing the same problem and it took around 4 hours for me to fix it and now it looks simple :) Scenario 1(simple): ======================= You have two images and want to show the 2nd one, in case 1st one is not valid. Use following image tag: <img src="'+src+'" onError="this.onerror=null;this.src=\'https://browshot.com/static/images/not-found.png\'" />;  Note: Please don't forget to add this.onerror=null otherwise browser may stuck in a loop. Scenario 1(typical): ======================= You have three images: 1. Show image1 if valid otherwise image2. 2. If image2 is also invalid show 3rd one. In this case we have to use above code as well as one more function for middle image. Not working -------------------- You can use following code in JS and looks like i