20-08-2008

Javascript Javascript check if file excists

For images you can use the js Image object's methods:

function testImage(URL) {
 	var tester=new Image();
  	tester.onload=function(){processImage(URL)};
  	tester.onerror=function(){errorHandler(URL)};
  	tester.src=URL;
}
 
function processImage(URL){
	alert(URL+' excists')
}
 
function errorHandler(URL){
	alert(URL+' does not excist')
}

For other files you can use an XMLHttpRequest like so:

function FileExists(strURL)
{
	oHttp = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP");
	oHttp.open("HEAD", strURL, false);
	oHttp.send();
	return (oHttp.status==404) ? false : true;
}

Comments:

Your comment:

»

 

[x]