Skip to content
Advertisement

Listen for global events on a Web Component

I have a main.js that makes a call to an API and receives a response object. After the response, I want to fire an event that my Custom Web Component is listening for.

JavaScript

This event works in the main document. I’ve attached a listener to the main document to test but it is not heard on my custom component.

JavaScript

As you can see from above, I’ve attached a listener both to the component (with this) and to its shadow root (for test purposes).

The event is not heard on the defined web component. I thought this might have something to do with the event capture phase (and maybe adding another flag to my custom event options object.

I’m still learning the ins and outs of Web Components and haven’t figured out this piece. Any help would be appreciated!

Advertisement

Answer

You are dispatching the event on document. The event will never reach the component since events are not sent to every element on the page.

In the capture phase the event goes from document down to the event that dispatched it, then the bubble phase walks the tree the other direction and goes from the element that dispatched it back towards document.

Either your component needs to add its event listener to document or your code would need to change to something like this:

JavaScript

But I really would not suggest doing that. Instead, if the event is going to be dispatched on document then you should listen for it on document.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement