1

I'm trying to change the src of some img tags if/when there is a problem with the pics when loading the page. I seem to be getting some random behavior particularly from Internet Explorer 9. Some of the images show the replaced image correctly and some have a red cross on them. If I debug in the browser I'm told that ImgError() is not defined. It's clearly defined in the code and is obviously working. Why is this happening?

<div class="PhotoBorder"><img alt="" onerror="imgError(this)" 
    src="./images/services/69_Staffmember.jpg" /></div>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js">           
    </script>  

<script> 
$(window).load( function() {
    $('.RowWrapper').each(function() {
        var TotalWidth = 0;
        $(this).find('.StaffMember').each(function() {
            TotalWidth = TotalWidth + $(this).outerWidth(true);     
        });
        this.style.width = TotalWidth + "px";           
    });
});

function imgError(img)
{ 
    img.setAttribute("src","./images/services/49_ImgMissing.jpg");  
    img.removeAttribute("onerror");
}     

</script>
4
  • TotalWidth.toString() + "px"; Commented Mar 31, 2014 at 1:25
  • 1
    The script defining ImgError must be before any code that calls it. HTML is parsed and "executed" sequentially. Commented Mar 31, 2014 at 1:41
  • What does this have to do with jQuery? Commented Mar 31, 2014 at 1:43
  • @RobG Your suggestion seemed to work. Please submit an answer and I will mark as correct. Commented Mar 31, 2014 at 2:50

3 Answers 3

1

Firstly, if you're getting the error that ImgError() is not defined it's because your function name is imgError, not ImgError().

But, if that was a typo in your question then it's possible that you're getting the error imgError() is not defined at some points because chances are the image is being requested from the cache and it's error handler is firing instantaneously - i.e. before the rest of your document has been parsed meaning that your imgError function is not yet available. To fix it, just put your imgError function in a script tag in the head of your document.

Sign up to request clarification or add additional context in comments.

4 Comments

Yes that was just a typo in the question.
Wow, seriously? I am using a content management system. I don't have access to the head section of the page.
Isn't that the whole point of document.ready or even window.load?? The document is supposed to be ready to work on.
@PaulMatthews - what part of your code has anything to do with $(window).load? the onerror attribute fires when there's an error requesting the image. That may be immediately or after a delay. Additionally, your imgError() function isn't inside any $(window).load (not that it would make a difference).
0

Give your image element an id and try this:

document.getElementById("imageid").src="./images/services/49_ImgMissing.jpg";

7 Comments

Can't use IDs because there are many images. The code is designed to work for each individual one.
are you using jquery?
@jp310 I think he is using jQuery based from the script code
or you can try: $(".your_class").attr("src","your_src");
I use jQuery in my code as you can see but not for this. I simply use the onError attribute to call the ImgError() function.
|
0

I'm answering on behalf of RobG. The definition of imgError() was below where it was being called in my code. You must position your code definitions above where you make calls in your html. Otherwise your results may be undefined as I found out.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.