Đang chuẩn bị nút TẢI XUỐNG, xin hãy chờ
Tải xuống
Tham khảo tài liệu 'phát triển javascript - part 24', công nghệ thông tin, kỹ thuật lập trình phục vụ nhu cầu học tập, nghiên cứu và làm việc hiệu quả | 10.2 Using Object Detection for Good 203 Listing 10.6 Unfriendly Host object behavior TestCase HostObjectTest test IE host object behavior function var xhr new ActiveXObject Microsoft.XMLHTTP assertException function if xhr.open Expectation property exists Reality exception is thrown assertEquals unknown typeof xhr.open var element document.createElement div assertEquals unknown typeof element.offsetParent assertException function element.offsetParent In his article Feature Detection State of the Art Browser Scripting 1 Peter Michaux provides the isHostMethod method shown in Listing 10.7 to help with feature detection and host methods. Listing 10.7 Checking if a host object is callable tddjs.isHostMethod function function isHostMethod object property var type typeof object property return type function type object object property type unknown return isHostMethod 1. http peter.michaux.ca articles feature-detection-state-of-the-art-browser-scripting Download from www.eBookTM.com 204 Feature Detection This method is able to recognize callable host objects based on the following observations ActiveX properties always have a typeof result of unknown. Non-ActiveX callable host objects in Internet Explorer usually have a typeof result of object. The boolean coercion is required to avoid null which also has a typeof result of object. In other browsers callable objects tend to have a typeof result of function even host methods Using this helper we can improve our cross-browser event handler as seen in Listing 10.8. Listing 10.8 Improved feature detection for addEventHandler function addEventHandler element type listener if tddjs.isHostMethod element addEventListener element.addEventListener type listener false else if tddjs.isHostMethod element attachEvent listener.call element.attachEvent on type function return listener.call element window.event else Possibly fall back to DOM0 event properties or abort 10.2.4 Sample Use Testing Testing for the existence and type of an .