While working on Twebst web automation library I encountered this problem: how to simulate setting the focus on HTML edit controls in Internet Explorer? There are two ways to do this.
- Call IHTMLElement2::focus() method on target element that "causes the element to receive the focus and executes the code specified by the onfocus event".
- Rise onfocus event on target element by calling IHTMLElement3::fireEvent() method.
The two approaches are quite similar but there are some interesting differences.
- fireEvent("onfocus") does not actually set the focus on the element, it just executes the code of the onfocus handler event.
- Calling focus method sets the focus on target element and call the onfocus event handler but not immediately. The onfocus event seems to be inserted in a queue and its handler is executed asynchronously after the current handler is finished.
- If focus method is called from inside the onfocus handler nothing happens if the control already has the focus (that prevents an infinite recursion).
window.status += "b";
window.status += "c";
window.status += "a";
<input type="text" onfocus="EditOnFocus()"; id="editTest"/><br/>
<input type="button" value="focus" id="btnFocus" onclick="BtnFocusClick();"/>
<input type="button" value="fire onfocus" id="btnOnFocus" onclick="BtnOnFocusClick();"/>
If pressing the button "fire onfocus" button the message in the Internet Explorer status bar is the expected one "ac". If pressing the "focus" button, the message is in reverse order than expected: "ba". That suggests that EditOnFocus handler is called after BtnFocusClick exit.