实例以下: javascript
if (typeof(obj) == "undefined") { html
// 操做语句} java
若是typeof(obj) == "undefined" 为true则该对象不存在,当前脚本尚未定义该对象
spa
其余的写法要注意: code
// 这种方法不起做用if (x == undefined)
// 操做语句
//
这个方法一样不起做用-
必须检查//
字符串 "undefined"
if (typeof(x) == undefined)
//
操做语句 htm
这样咱们就要知道什么样的状况下会返回的是undefined呢?有以下两种: 对象
实例: blog
<html>
<body>
<script type="text/javascript">
if (typeof(ActiveXObject)=="undefined")
{alert("不存在ActiveXObject")}
else
{alert("存在ActiveXObject!!")} ip
</script> 字符串
</body>
</html> //返回的是:存在ActiveXObject,由于该对象是js里已经制定的对象已经由js定义声明过的,只要是js制定存在的都是非 undefined
可是若是改成:typeof(ActiveXObject1)=="undefined” 则返回的是不存在了ActiveXObject1
判断元素是否存在:
<script language="javascript"> function changelocation() { if(document.getElementById("payment_info")) alert('true'); else alert('false'); } </script>
<body onload="changelocation()"> <div id="payment_info1" > </div> </body>