---
schema_version: "1"
id: bc99bf1fa706f204d3d67989e2c0f9643bed32e688dc2118d1fc86802b5abe91
title: Event Handler Behavior for Active Components
collection: developer
version: current
locale: en-us
portal_url: https://docs.salesforce.com/developer/current/en-us/platform/aura-platform/guides/aura/events/event-handler-active-components.md
canonical_url: https://developer.salesforce.com/docs/platform/aura-platform/guide/event-handler-active-components.html
source_site: developer.salesforce.com
content_type: documentation
content_hash: 20d172dc80bc0ee384384c92c8270fc1c8a64dc40bfa342300c31789f19916c3
products:
  - Lightning Aura Components Developer Guide
  - Platform Developer Content
---

# Event Handler Behavior for Active Components

To prevent active event handlers on cached pages from causing problems, add a workaround to your code to check if the component is still visible. To avoid this scenario and the workaround, use Lightning message service instead to communicate across the DOM within a Lightning page. The default scope used by Lightning message service channels publishes only to active components.

When navigating away from a page in Lightning Experience, the framework caches the components in the page so that they remain active, along with their event handlers. This caching speeds up navigation, but it can cause the cached component to respond to events that are not intended for it, such as `force:refreshView` or `force:recordSaveSuccess`.

This workaround uses the `offsetParent` property for the component to get its handlers while they’re visible. The workaround is good only if the component definition has an HTML element in it.

This component includes an event handler and some HTML.

```html
<!--myComponent.cmp-->
<aura:component>
  <aura:handler event="c:appEvent" action="{!c.onEvent}>
  <h1>This component has a handler</h1>
</aura:component>
```

Here’s the client-side controller that uses the `offsetParent` property to get the component’s handlers while they’re still visible.

```javascript
/* myComponentController.js */
({
  onEvent: function(component, event, helper) {
    var elem = component.getElement();
    if (elem && elem.offsetParent !== null) {
      // event handling logic here
    }
  }
})
```

**See Also**  


-   [Communicating Across the DOM with Lightning Message Service](https://developer.salesforce.com/docs/platform/aura-platform/message-channel/message-channel-intro.md)
-   [*Component Library:* Message Service](https://developer.salesforce.com/docs/component-library/bundle/lightning-message-service/documentation)

