This function returns true if the file exists in the given URL. The below mentioned code is optimized to be use with Google Chrome also.
Instead of "return true, return false", "return 1, return 0" is used - this is because Google chrome avoids false and true statements.
function file_exists (url) {
// Returns true if filename exists
var req = this.window.ActiveXObject ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest();
if (!req) {throw new Error('XMLHttpRequest not supported');}
// HEAD Results are usually shorter (faster) than GET
req.open('HEAD', url, false);
req.send(null);
if (req.status == 200){
return 1;
}
return 0;
}
October 12, 2009
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment