javascript_利用eval反射验证方法是否存在
2012-02-20 23:09
471 查看
<html>
<head>
<style type="text/css">
div
{
border:thin solid green;
width:200px;
height:100px;
}
</style>
<script type="text/javascript">
function hideOverflow()
{
document.getElementById("div1").style.overflow="hidden";
}
</script>
</head>
<body>
<script>
try{
alert(typeof(eval("hideOverflow")));
} catch(e) {
if(e.name!="ReferenceError")alert(e);
}
alert(1);
</script>
<div id="div1">
This is some text. This is some text. This is some text.This is some text. This is some text. This is some text.This is some text. This is some text. This is some text.
</div>
<br />
</body>
</html>
注意:eval("hideOverflow")会执行hideOverflow方法如果hideOverflow不存在该方法就回直接出错,
所以使用try..catch来捕获异常,这是一个ReferenceError,
typeof(eval("hideOverflow"))择会是一个function,可以利用这个来判断:
if(typeof(eval("hideOverflow"))=="function")alert("这是一个fun");
<head>
<style type="text/css">
div
{
border:thin solid green;
width:200px;
height:100px;
}
</style>
<script type="text/javascript">
function hideOverflow()
{
document.getElementById("div1").style.overflow="hidden";
}
</script>
</head>
<body>
<script>
try{
alert(typeof(eval("hideOverflow")));
} catch(e) {
if(e.name!="ReferenceError")alert(e);
}
alert(1);
</script>
<div id="div1">
This is some text. This is some text. This is some text.This is some text. This is some text. This is some text.This is some text. This is some text. This is some text.
</div>
<br />
</body>
</html>
注意:eval("hideOverflow")会执行hideOverflow方法如果hideOverflow不存在该方法就回直接出错,
所以使用try..catch来捕获异常,这是一个ReferenceError,
typeof(eval("hideOverflow"))择会是一个function,可以利用这个来判断:
if(typeof(eval("hideOverflow"))=="function")alert("这是一个fun");
相关文章推荐
- Javascript 判断是否存在函数的方法
- 利用javascript判断文件是否存在
- 多种方法判断Javascript对象是否存在
- 想到一个对于灵魂是否存在的简单可行的验证方法
- javascript(jquery)中判断一个元素在页面中是否存在的方法(转载-来自CSDN)
- 利用JavaScript手动触发ASP.Net validator验证控件的方法
- 利用javascript判断文件是否存在
- 利用反射实现ASP.NET控件和数据实体之间的双向绑定,并且在客户端自动验证输入的内容是否合法
- ajax同步验证单号是否存在的方法
- 利用javascript验证邮箱地址是否合法
- 利用javascript验证输入框中的值是否为日期格式[网上找的]
- 利用ajax技术验证数据是否存在的一个简单例子
- PHP自带方法验证邮箱是否存在
- javascript 判断方法是否存在
- javascript检测对象中是否存在某个属性判断方法小结
- jQuery异步验证用户名是否存在的方法
- 判断身份证号码是否正确javascript、asp两种验证方法
- Javascript 判断是否存在函数的方法
- JavaScript 验证浏览器是否支持javascript的方法小结
- 用JavaScript的正则表达式验证网址格式是否正确,方法很多,这只是一个实现的方法.