onFocus() for Salesforce Classic

Registers a function to call when the browser focus changes. In the Salesforce console, the browser focus changes when a user navigates between primary tabs or the navigation tab.

NameTypeDescription
listenerfunctionJavaScript method called when the browser focus changes.

This method is asynchronous. The response is returned in an object passed to a callback method. The response object contains the following fields.

NameTypeDescription
resultstringReturns the URL of the page in focus as a JSON string, including any applicable object ID, object name, object type, and for API version 33.0 or later, the object tab name. If the page isn’t focused on an object, the object ID, object name, and object fields are empty. For API version 31.0 and later, invoking this method on a PersonAccount object also returns accountId or contactId, and personAccount (true if the object is a PersonAccount, false otherwise).
errorstringIf the API call was successful, this variable is undefined. If the API call failed, this variable returns an error message.

Example result:

Example result for a PersonAccount object (API version 31.0 and later):