Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

onerror <img> tag attribute always fires in IE, why?

I have code something like this

<a class="img" href="LINK">
  <img src="GOOD_IMG" title="title" onerror="src='ERROR_IMG'">
</a>

in FireFox and chrome it behaves as you would expect (shows GOOD_IMG if it exists and shows ERROR_IMG if not) but in IE (9) it always shows the ERROR_IMG.

If I debug in IE and on the fly set the onerror so something else e.g.

onerror="alert('error')" 

then the alert message appears and the correct image is shown.

What could be causing IE to cause onerror to activate where the other browsers don't have a problem?

Is there someway I can find out what is causing the onerror?

Thanks

like image 588
Chris Avatar asked Apr 09 '13 16:04

Chris


People also ask

What is Onerror in IMG tag?

Definition and Usage The onerror event is triggered if an error occurs while loading an external file (e.g. a document or an image). Tip: When used on audio/video media, related events that occurs when there is some kind of disturbance to the media loading process, are: onabort.

What does onerror do?

onerror is a special browser event that fires whenever an uncaught JavaScript error has been thrown. It's one of the easiest ways to log client-side errors and report them to your servers. It's also one of the major mechanisms by which Sentry's client JavaScript integration (raven-js) works.

Is IMG Onerror deprecated?

onerror is still valid, and seemingly, they haven't classified the tag attribute event as deprecated there (in that section) yet, they're still showing an img attribute example. So you can still use it, but just don't use it as a tag attribute, you can place it inside a script tag or a separate file.

What is Onerror in HTML?

The onerror attribute fires when an error occurs while loading an external file (e.g. a document or an image).


2 Answers

I also experienced similar symptoms. In my case, 'onerror' occurred by putting 'empty' value in src at <img>.

Problem:

html

<img src="" onerror="this.src='ERROR_IMG'">

js

$('._profile_image:first').attr('src', IMAGE_URL);

Solution:

<img onerror="this.src='ERROR_IMG'">
like image 185
Yeongjun Kim Avatar answered Sep 29 '22 15:09

Yeongjun Kim


You can try like this. First you have to write one JS function for checking whether the image is exists or not (AJAX call return exists or not) and change the image accordingly.

Second you call the function on onerror event

function imgError(imageControl, path) {        
            $.ajax({
                type: "POST",
                async: true,
                url: "test.aspx/CheckImageExists",
                data: "{'imagePath':" + JSON.stringify(path) + "}",
                contentType: "application/json; charset=utf-8",
                success: function (response) {
                    if (response.d == "exists") {
                        imageControl.src = path;
                    }
                    else {
                        imageControl.src = 'img/errorimg.jpg';
                    }
                }
            });
            return true;
        }

<img src="<%# "admin/"+ Eval("imagath") %>" onerror="imgError(this,'<%# "admin/"+ Eval("imagath") %>')">

C#
 [WebMethod]       
        public static string CheckImageExists(string imagePath)
        {
            string fullPath = HttpRuntime.AppDomainAppPath.ToString() + imagePath;
            fullPath=fullPath.Replace('/','\\');
            return File.Exists(fullPath) ? "exists" : "notexists";           
        }
like image 21
Jain Avatar answered Sep 29 '22 15:09

Jain