_util.RegisterForEvent Method
Use this method in a JavaScript function that controls listening to events to support recording. UFT calls this method when registering event listeners.
You can also invoke this method from event handlers. If the event changes the DOM by adding elements to the control, you may need to listen to events on the new elements. For example, opening a submenu may add new menu elements whose events must be recorded.
JScript | |
---|---|
public function RegisterForEvent( element : Object, eventName : String, handler : String, handlerParam : Object ); |
Parameters
- element
- A reference to the element object.
- eventName
- The event name.
- handler
- The name of the event handler method. UFT calls this method when the event occurs. You must implement a JavaScript function by this name to handle the event.
- handlerParam
The parameter to pass to the event handler method.
Optional.
The following JavaScript function is used to register for the Click event on ASP .NET AJAX DropDownList items. When a DropDownList item is clicked, the OnSelect method is called (with the item text as a parameter) to handle the event.
function ListenToEvents( elem )
{
var DropDownListObj = _elem.parentNode.nextSibling.nextSibling;
for (var childIndex = 0; childIndex < DropDownListObj.childNodes.length; childIndex++)
{
// Do not register to events on #text child nodes (Type = 3) since they do not fire events
if (DropDownListObj.childNodes[childIndex].nodeType != 3)
{
//Save the selected item's data to use as the item name for the handler parameter
var Item = DropDownListObj.childNodes[childIndex].firstChild.data;
_util.RegisterForEvent( DropDownListObj.childNodes[childIndex] , "onclick" , "OnSelect", Item );
}
}
return true;
}
// This function is called when the Click event is performed on an item of the Drop Down list
function OnSelect( handlerParam , eventObj )
{
var arr = new Array();
arr[0] = handlerParam;
//The handler parameter is the name of the selected item
_util.Record( "Select", toSafeArray(arr), 0);
return true;
}