I am changing src for image dynamically and want to capture on load event for that image. Is there a way I can do it?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Several years ago, I discovered that the onload event was not reliable in some browsers (I don’t remember which ones) when setting
.srcfor the second time. As such, I settled for replacing the image object. I would create a new image object, set the onload handler, set the.srcvalue and then when it loads, insert it into the page in place of the existing image.When creating an image from scratch in javascript, you do this:
If you just want to change the
.srcof an image, you just find the DOM object in the page and set it’s.srcproperty.If you want to try to capture the onload event when resetting the
.src(what I had reliability problems with a couple years ago, you would do this:If you want to load a new image and replace an existing one with it when it loads, you would do this:
And, you would call this like this: