p; } } // 返回值为一个数字或null return result; } function reportImageArea() { // 现在可以判断返回值 var imgArea = getImgAreas(); var output; if (imgArea == null) { // 对于不支持images对象的浏览器也要给出相应信息 output = "Unknown"; } else { output = imgArea; } document.reportForm.imgData.value = output; } 这样,不管浏览器是否支持该对象,都能给用户比较合理的信息,而不会跳出突兀的错误信息。
五、检测浏览器对特定属性和方法的支持 问题: 检测一个对象是否含有某个特定的属性或方法。 解决方案: 大多数情况下,可以用类似于下面的代码来判断: if(objectTest && objectPropertyTest) { // OK to work with property } 先检测对象是否存在,然后再检测对象的属性是否存在。如果对象确实不存在,该方法有效;如果属性存在,但其值为null, 0, false,if语句求值的结果也将是false!所以这种方法并不安全,最好的方法是这样: if (objectReference && typeof(objectReference.propertyName) != "undefined") { // OK to work with pro 上一页 [1] [2] [3] [4] [5] 下一页 |