Click here to Skip to main content
15,891,687 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Does any one help me how to write a code whether the image is exist inside the folder or not using JAVASCRIPT

and also how we handle it if the image is doesn't exist inside the folder
Posted
Updated 11-Feb-15 22:15pm
v2

function urlExists(testUrl) {
 var http = jQuery.ajax({
    type:"HEAD",
    url: testUrl,
    async: false
  })
  return http.status;
      // this will return 200 on success, and 0 or negative value on error
}


Then use

if(urlExists('urlToImgOrAnything') == 200) {
    // success
}
else {
    // error
}
 
Share this answer
 
Comments
venkat teja 12-Feb-15 6:32am    
hello thank you but i need pure javascript code
[no name] 12-Feb-15 6:38am    
hello, if you use simple .error() function then some browser not accept this. My solutions is work in all browser.
Try
JavaScript
function testImage(URL) {
    var tester=new Image();
    tester.onLoad=isGood;
    tester.onError=isBad;
    tester.src=URL;
}

function isGood() {
    alert('That image exists!');
}

function isBad() {
    alert('That image does no exist!');
}


Script taken from here - http://www.irt.org/script/52.htm[^].
 
Share this answer
 
Comments
venkat teja 12-Feb-15 5:37am    
Hello ty but i need pure javascript

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900