JS代碼如下
/**
* 判斷服務的文件是否存在
* @param filepath 文件地址
*/
function isExistFile(filepath){
if(filepath == null || filepath == undefined || filepath == ""){
return false;
}
var xmlhttp;
if (window.XMLHttpRequest){
xmlhttp = new XMLHttpRequest();
}else{
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("GET",filepath,false);
xmlhttp.send();
if(xmlhttp.readyState == 4){
if(xmlhttp.status == 200){
console.clear();
return true;
}else{
console.clear();
return false;
}
}
}
項目中使用
if(!isExistFile(obj['jzjz'])){
obj['jzjz'] = "image/head.png";
}