addEventListener()

Adds a listener for a custom event type or a standard event type when the event is fired. This method adds a listener for custom event types in API version 25.0 or later; it adds a listener for standard event types in API version 30.0 or later.

For the list of standard events, see Methods for Console Events.

Syntax 

1sforce.console.addEventListener( eventType: String, eventListener:Function, (optional)additionalParams:Object )

Arguments 

NameTypeDescription
eventTypestringCustom event type for which eventListener listens.
eventListenerfunctionJavaScript method called when an eventType is fired.
additionalParamsobjectOptional parameters accepted by this method. The supported properties on this object are tabId: The ID of the tab to listen for the specified event. This argument is only available in API version 30.0 or later.

Sample Code API Version 25.0 or Later–Visualforce 

1<apex:page>
2    <apex:includeScript value="/support/console/68.0/integration.js"/>
3    <script type="text/javascript">
4
5        var listener = function (result) {
6            alert('Message received from event: ' + result.message);
7        };
8        //Add a listener for the 'SampleEvent' event type
9        sforce.console.addEventListener('SampleEvent', listener);
10    </script>
11</apex:page>

Response 

This method is asynchronous, so it returns its response in an object in a callback method. The response object contains the following field:

NameTypeDescription
messagestringThe message which is sent with the fired event. If the response is from a custom keyboard shortcut, the message includes the following information on which the browser is focused, in this order:
  1. Object ID of the primary tab
  2. ID of the primary tab
  3. Object ID of the subtab
  4. ID of the subtab

Sample Code API Version 30.0 or Later–Visualforce 

1<apex:page>
2    <apex:includeScript value="/support/console/68.0/integration.js"/>
3    <script type="text/javascript">
4
5        var onEnclosingPrimaryTabClose = function (result) {
6            alert('The enclosing primary tab is about to be closed. Tab ID: ' + result.id + ', Object ID: ' + (result.objectId ? result.objectId : 'not available'));
7        };
8
9        //Add a listener to handle the closing of the enclosing primary tab 
10        sforce.console.getEnclosingPrimaryTabId(function (result) { 
11            if (result.id) {
12                sforce.console.addEventListener(sforce.console.ConsoleEvent.CLOSE_TAB,
13                onEnclosingPrimaryTabClose, { tabId : result.id });
14            } else {
15                alert('Could not find an enclosing primary TAB!');
16            }
17        });
18    </script>
19</apex:page>

Response 

This method is asynchronous, so it returns its response in an object in a callback method. The response object contains the following field:

NameTypeDescription
messagestringThe message which is sent with the fired event. If the response is from a console event, the message includes payload details as described in Methods for Console Events. If the response is from a custom keyboard shortcut, the message includes the following information on which the browser is focused, in this order:
  1. Object ID of the primary tab
  2. ID of the primary tab
  3. Object ID of the subtab
  4. ID of the subtab