---
schema_version: "1"
id: ec917e7579e9414dcb1b2f5502f4300a9912c2d286e013529aec08c02e0ff505
title: onFocus() for Salesforce Classic
collection: developer
version: current
locale: en-us
portal_url: https://docs.salesforce.com/developer/current/en-us/service-cloud/api-cti/guides/sforce-api-cti-onfocus.md
canonical_url: https://developer.salesforce.com/docs/service/api-cti/guide/sforce-api-cti-onfocus.html
source_site: developer.salesforce.com
content_type: documentation
content_hash: edf3ccd6999042c76da2bdf8b478a34b5a8cfdd8ab095124d67ae82f822e7a16
products:
  - Open CTI Developer Guide
  - Service Cloud Developer Content
---

# onFocus() for Salesforce Classic

## Usage

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.

## Syntax

```javascript
sforce.interaction.onFocus( listener:function );
```

## Arguments

|Name|Type|Description|
|------|------|-------------|
|`listener`|function|JavaScript method called when the browser focus changes.|


## Sample Code–JavaScript

<!-- javascript set_clipboard -->
```javascript
<head>
   <script type="text/javascript"
src="http//domain:port/support/api/25.0/interaction.js"></script>
   <script type="text/javascript">
     var callback = function (response) {
           if (response.result) {
                  alert(response.result);
} };
       function onFocus() {
       //Invokes API method
       sforce.interaction.onFocus(callback);
       }
</script>
</head>
<body>
       <button onclick="onFocus();">onFocus</button>
</body>
```

## Response

This method is asynchronous. The response is returned in an object passed to a callback method. The response object contains the following fields.

|Name|Type|Description|
|------|------|-------------|
|`result`|string|Returns 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).|
|`error`|string|If the API call was successful, this variable is undefined. If the API call failed, this variable returns an error message.|


Example `result`:

```javascript
{"url":"http://MYDOMAINNAME.my.salesforce.com/001x0000003DGQR",
"objectId":"001x0000003DGQR","objectName":"Acme",
"object":"Account","displayName":"Company"}
```

Example `result` for a PersonAccount object (API version 31.0 and later):

```javascript
{"url":"http://MYDOMAINNAME.my.salesforce.com/001x0000003DGQR",
"objectId":"001x0000003DGQR","objectName":"Acme Person Account",
"object":"Account", "contactId":"003D000000QOMqg", "personAccount":true}
```

