I am looking to change the source of an image or several images. My website displays a table of ten images (01.jpg, 02.gif… 10.jpg) I want the html or JavaScript to change the image source according to what is available. For instance if the 01.gif is not available then to default to the 01.jpg picture. If the 01.jpg is not available to default to an error.jpg. I initially had this html:
[CODE]<img src=”01.gif” id=”01″ alt=”pic” onerror=”src=’01.jpg'”>
Then I tried some JavaScript to include the error image and received an error message. Here is the code:
[CODE]<script type=”text/javascript”>
function changesource(){
try
{
document.getElementById(“01”).setAttribute(“src”, “01.jpg”);
}
catch(error)
{
document.getElementById(“01”).setAttribute(“src”, “error.jpg”);
}
}
</script>
</head>
<body>
<img src=”01.gif” id=”01″ alt=”pic” onerror=”changesource()”>
</body>
</html>
I understand the “Stack overflow” error message I am receiving however I am not sure of another way to go through the images. Any suggestions will be very helpful. Thanks.