I am trying to get contact details from phonegap for android, here I am trying to get a contact image and try to replace it with the default image if the actual contact photo does not exist.
now when we try to access the photo, it returns the url even if the photo does not exist. so I'm trying to create an object here Imageand assign a photo url from the phone screen saver, if the image does not exist, the object Imagewill raise an exception onerrorand inside that I change its attribute srcto the default image.
but now the problem is that the functions onerrorwork late, I mean, when the data is loaded, so the attribute srcdoes not change. so can anyone tell me how to changeonerror
if(contacts[i].photos){
for(k =0 ;k < contacts[i].photos.length;k++){
var imgVal = new Image();
imgVal.onerror = function(){
this.src= 'resources/images/default_usr.png';
this.error = null;
contactData.setContactImage(this.outerHTML);
console.log("2");
}
imgVal.width="45";
imgVal.height="45";
imgVal.src = contacts[i].photos[k].value;
contactData.setContactImage(imgVal.src);
console.log("1");
}
}
source
share