2

Hello I'm using regular img:

<img border="0" title="Sterling Silver Jesus Name Necklace" src="http://www.myengravedjewelry.com/images/products/101-01-071-02_medium.jpg" alt="0">

I just want to put another url in case if this one is failed to load of some how to check if this url is bad than change it (with javascript)

3 Answers 3

5

Use attribute onerror

<img src="bad_src.jpg" onerror="this.src='good_src.jpg'">
Sign up to request clarification or add additional context in comments.

Comments

1

you can use method like this

javascript

function img404(img) {
    img.onerror = "";
    img.src = "default.jpg";
    return true;
}

html

<img src="image.png" onerror="img404(this);"/>

Comments

0

There are no such methods in HTML. You can control from some back end language like PHP.

Something like:

if(!file_exists($filepath)) {
    $src = 'http://site.com/def.jpg';
}

Moreover, use proper image description in ALT tag or image.

1 Comment

and in asp.net javascript?

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.