Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

javascript detect image

Posted on 2005-03-16
3
Medium Priority
?
311 Views
Last Modified: 2013-12-16
Hi there,

I have this situation where i am pulling in external images on a webpage from a different server to where the pages are hosted,  Now since its a dynamic site if the image is not located on the server where the URL is pointing it just shows the annoying blank frame with a red X in it.

I need to know if there is any way of using javascript or any other language for that matter for somehow detecting if the image exists;

so basically;

if image exists { show picture of image } else {
if image does not exist { show another image saying 'no image' }

is this possible?

Thanks,

Ian
0
Comment
Question by:webpolsol
3 Comments
 
LVL 32

Accepted Solution

by:
Batalf earned 1000 total points
ID: 13555070
Try to put in an onerror

<img src="yourImage.gif" onError= src="errorImage.gif">
0
 
LVL 13

Expert Comment

by:davidlars99
ID: 13555421
and also

<html>
<head>
<script>
var myImage=new Image();
myImage.onerror=myFunction;
myImage.src='myImage.gif';

function myFunction(){
   
    alert(this.src+' not found!');

}

</script>
</head>
<body>
<form name="test" method="post">
   <input type="button" onClick="myFunction()" value="test me" mane="test" />
</form>
</body>
</html>
0
 
LVL 1

Author Comment

by:webpolsol
ID: 13556285
Works perfectly!

Many Thanks,

Ian
0

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
No other job is as rewarding and demanding as building an iPhone app is. It is not really in the hands of the developer for the success of an iPhone app. Many factors operate jointly for every iOS application's success in the market.
The purpose of this video is to demonstrate how to set up the permalinks on a WordPress Website. This will be demonstrated using a Windows 8 PC. Go to your WordPress login page. This will look like the following: mywebsite.com/wp-login.php : Go t…
The viewer will learn how to dynamically set the form action using jQuery.
Suggested Courses
Course of the Month11 days, 2 hours left to enroll

571 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question