How to get the close window event?

Hello, I want to trigger a fuction when a user close the Chatbot window. How can I get the event “close window” in js ?

Thanks

2 Likes

Hi @Frederic_Revellat :waving_hand:

Sure, here is the code for creating the close window event in the Custom JS:

util.addListener(".es-window-close-button", "click", () => {
  // Your code goes here
});

If you need further help with the widget customization, just describe your idea. Our devs will gladly help you with it :wink:

1 Like

Thanks a lot, it works well !

2 Likes

You’re always welcome!

By the way, we’d like to invite you to participate in our September Contest, where you can win a 6-month extension for your subscription - September Contest: Best AI Feature Ideas Win 6 Months FREE!:wrapped_gift:

Check the details and join in :wink:

1 Like

I have an other question about it, I’d like to catch the escape event too when you press “esc” on the keyboard ?

2 Likes

You’d like to close the chat window when pressing “Esc”, am I right?

1 Like

I wanted to catch the escape event to close an overlay. I found a way to do it:

document.addEventListener("keydown", (e) => {
2 Likes

Ah, I see! Glad to see you’ve managed to achieve this, and thanks for sharing the solution here :wink:

1 Like

I did it, it used to work but now it does not work anymore.

util.addListener(“.es-window-close-button”, “click”, () => {overlayDiv.style.display = “none”;console.log(‘close window.’);});
1 Like

Hi there, @Frederic_Revellat :waving_hand:

Apologies for the inconvenience!

After a recent update, the class of the close button element was changed from .es-window-close-button to .es-header-button-close.

I’ve updated the script in your widget. Here is how it looks now:

util.addListener(".es-header-button-close", "click", () => {
  overlayDiv.style.display = "none";
  console.log('close window.');
});

Please check it out and let me know how it’s working now :slightly_smiling_face:

Sorry it does not work still, I don’t even get any log on the console

fyi this code still works well:

document.addEventListener("keydown", (e) => {
  if (e.key === "Escape") {
    overlayDiv.style.display = "none";
  }
});
1 Like

We’ve reviewed the code with the devs. The thing is that the closing event occurs before the click registration event . Please try to use this code instead :slightly_smiling_face:

document.addEventListener('click', (e) => {
	if (e.target.closest('.es-header-button-close[aria-label="Close"]')) {
        overlayDiv.style.display = "none";
		console.log('close window.');
	}
});
2 Likes

Perfect it works well ! thank you so much for your reactivity, 5-star support !

1 Like

Amazing, thanks a ton for your warm feedback!

If anything else comes up, we’ll be happy to assist :wink:

1 Like