Back to Snippets

AI Chatbot Popup Widget JS

A chat widget for any website. The round button in the corner opens a panel with a greeting and three suggested questions, and the demo opens it on load so you can try it right away. Type a question or tap a suggestion, and three dots show while the reply is on its way. The new chat button starts over, and the close button or Esc hides the panel again.

Every reply comes from getReply, a function that returns a Promise of text. In the demo it matches keywords against three built-in answers after a short delay, and those answers explain the widget itself. Swap its body for a fetch to your own server route that calls a model, and the rest works unchanged. If the promise rejects, the chat shows a short error instead of waiting forever. Messages are added as plain text with append(), so whatever the model returns or the visitor types is never parsed as HTML.

The launcher has aria-expanded and aria-controls, the message list is a log region, and each message starts with a hidden You or Assistant label for screen readers. Focus moves to the input when you open the panel and back to the launcher when you close it. At 480 pixels wide or less the panel fills the screen and the page behind it becomes inert, so Tab stays in the chat. The main colors are custom properties on .chat-widget.

0
#javascript
#css
#ai
#chatbot
#chat widget
#popup
#messages
#accessibility