/*---------------------------------------------------------------------------*/
/* 判断函数或对像是否存在 一定要添加try catch块,否则不起作用 */
/* */
/* Javascript 的 typeof可以获取变量的类型,有如下6种返回值: */
/* 1)number; */
/* 2)string; */
/* 3)boolean; */
/* 4)object; */
/* 5)function; */
/* 6)undefined. */
/*---------------------------------------------------------------------------*/
/*对像是否存在*/
function objExists(objName){
try{
if(typeof eval(objName)=="undefined"){return false;}
if(typeof eval(objName)=="object"){return true;}
}catch(e){
return false;
}
}
/*函数是否存在*/
function funExists(funName){
try{
if(typeof eval(funName)=="undefined"){return false;}
if(typeof eval(funName)=="function"){return true;}
}catch(e){
return false;
}
}
/*ID是否存在*/
function IdExists(idName){
try{
if(document.getElementById(idName)){return true;}else{ return false;
}
}catch(e){
return false;
}
}
JS对象与函数检测
本文提供了一种在JavaScript中检查对象、函数及元素ID是否存在的方法。通过使用typeof操作符结合try-catch语句,确保了检测过程的安全性和准确性。

被折叠的 条评论
为什么被折叠?



