Replace the default image with a contact photo when the image does not exist in the phone stumble

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");       

                        }//end for contact photo
                    }// end if contact photo
+2
source share
2 answers

Give it a default background image, which is a contact photo. than letting you upload a photo to it, if there is a real photo, it will be uploaded to it.

html code:

<img src="Path/to/image" id="img"/>

css would be something like this:

#img{background: url(path/to/default/image) no-repeat scroll 0 0 transparent; backgorund-size:100% 100%; height:45px; width:45px; display:block;}
0
source

You didn’t show how you create contactData, and I didn’t do it myself, but here are a few considerations from reading your code, assuming your debug console is currently showing

1
2

until the image is assigned.

  • contactData.setContactImage(imgVal.src); image src (URL- ); this.src, , , imgVal, outerHTML setContactImage. : this.src , , , , Image. , .

  • , javascript , .. imgVal onerror, imgVal;

  • console.log(imgVal) , ( , setContactData ); ;

  • onerror ; , . onerror ( onerror) - , ; , Image onerror.

0

All Articles