Onerror <img> tag attribute always fires in IE, why?
I have code 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 expected (shows GOOD_IMG if it exists, and shows ERROR_IMG if not), but in IE (9) it always shows ERROR_IMG.
If I debug in IE and install onerrorsomething else on the fly , for example.
onerror="alert('error')"
A warning message appears and the correct image is displayed.
What can cause IE onerrorto activate where other browsers have no problem?
Is there any way to find out what causes onerror?
thank
+5
3 answers
. JS , ( AJAX-) .
onerror
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";
}
+1