Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
Bedrock Mission!

Learn more

View all

Sign in to view all badges

Adobe Experience Fragments - Event Handlers Not Consistently Being Attached When Injected by Target

Avatar

Level 2

Hello all:

We have an Experience Fragment that utilizes custom AEM Components. Some of the elements in one of these Components get an event handler attached to them through a "clientlib" JS script in AEM. When using these Components through AEM, the event handlers are attached consistently and the code works as expected.

 

The issue arises when we export the XF to Target and use it in an Activity. Approximately 90% of the time, the code works fine. The events are attached and client-side behavior is as expected. However, a minority of the time, we noticed that an element was not responding to user interaction as expected. After some troubleshooting, I found that this was because the Event Handlers from the JS script were not being attached even though the JS script was properly loaded in the page.

 

I assume it is a timing issue; e.g. the Component does not exist at the time the JS script attempts to attach the Event Handler. Is there any way to troubleshoot or potentially fix this issue (i.e. through an AEM setting or Target) without touching the JS code itself?

3 Replies

Avatar

Community Advisor

Hi,

If you are using jquery then this is the preferred way to bind to an existing or yet-to-exist element.

$('#newLinks').on("click", 'a', function() {
  alert("clicked");
});

Avatar

Level 2

Thanks for the response. I understand there are ways to modify the JS to address this, but is there anyway to do so without modifying that JS directly; for example, controlling when the XF gets injected, etc.

Avatar

Community Advisor

Hi,

I am not sure if you can load Target data as part of the source but I think it is loaded asynchronously.

and Asynchronous call requires race condition handling.