Programming Tips - javaScript: How can I find out of a file exists

Date: 2009jun23 Language: javaScript Platform: web Q. javaScript: How can I find out of a file exists A. Do Ajax with the HEAD command like this:
// Helper function getAjax() { return (!window.XMLHttpRequest)? (new ActiveXObject('Microsoft.XMLHTTP')):(new XMLHttpRequest()); } function isExists(url) { let req; req = getAjax(); try { req.open('HEAD', url, false); // We block (ie wait for the return) req.send(null); } catch(e) { return false; // for IE6 } return req.status == 200; } function exampleUse() { for (let i = 1; i < 10000; i++) { padded = zeroPad(i, 5); if (!isExists('http://www.example.com/photos/DSC' + padded + '.JPG')) break; } const n = i - 1; alert('There are ' + n + ' photos'); }