271Chapter 15Generic HTML Element ObjectsThe version with attachEvent()is (Web hosting bandwidth)

271Chapter 15Generic HTML Element ObjectsThe version with attachEvent()is as follows: myObject.attachEvent( onmousedown , setHilite); Both parameters are required. The first parameter is a string version (case-insensitive) of theevent name. The second is a reference to the function to be invoked when the event fires forthis object. A function referenceis an unquoted, case-sensitive identifier for the function withoutany parentheses (which also means that you cannot pass parameters in this function call). There is a subtle benefit to using attachEvent()over the event property binding approach. When you use attachEvent(), the method returns a Boolean value of trueif the event bind- ing succeeds. IE triggers a script error if the function reference fails, so don t rely on areturned value of falseto catch these kinds of errors. Also, there is no validation that theobject recognizes the event name. If you have used attachEvent()to bind an event handler to an object s event, you can dis- connect that binding with the detachEvent()method. The parameters are the same as forattachEvent(). The detachEvent()method cannot unbind events whose associations areestablished via tag attributes or event property settings. The W3C DOM event model provides functionality similar to these IE-only methods: addEventListener()and removeEventListener(). ExampleUse The Evaluator (Chapter 13) to create an anonymous function that is called in response toan onmousedownevent of the first paragraph on the page. Begin by assigning the anonymousfunction to global variable a(already initialized in The Evaluator) in the upper text box: a = new Function( alert( Function created at + (new Date()) + ) ) The quote marks and parentheses can get jumbled easily, so enter this expression carefully. When you enter the expression successfully, the Results box shows the function s text. Nowassign this function to the onmousedownevent of the myPelement by entering the followingstatement into the upper text box: document.getElementById( myP ).attachEvent( onmousedown , a) The Results box displays truewhen successful. If you mouse down on the first paragraph, analert box displays the date and time that the anonymous function was created (when the newDate()expression was evaluated). Now, disconnect the event relationship from the object by entering the following statementinto the upper text box: document.getElementById( myP ).detachEvent( onmousedown , a) Related Items:addEventListener(), detachEvent(), dispatchEvent(), fireEvent(), removeEventListener()methods; Event binding (Chapter 14). blur() focus() Returns:Nothing. Compatibility:WinIE3+, MacIE3+, NN2+, Moz1+, Safari1+ The blur()method removes focus from an element, while the focus()method gives focusto an element. Even though the blur()and focus()methods have been around since theearliest scriptable browsers, not every focusable object has enjoyed these methods since theelementObject.blur()