Add Chatbot to Website HTML Buttons: Open the Chat, Ask a Question or Pass a Logged-In Name From Your Own Code

Add chatbot to website HTML with one script tag and you get a floating bubble. Add a few more lines and the chat starts working with your page: a “Chat with us” link in your menu, an “Ask about delivery” button beside the price that sends the question for the visitor, a logged-in customer’s name passed to your team automatically, and a Google Tag Manager event when someone leaves their details. This guide covers the LiveChatBot JavaScript API from plain HTML, the one timing trap that breaks most custom buttons, and a builder that writes the code and lets you try the buttons before you paste anything.

LiveChatBot.open()
LiveChatBot.sendMessage(“Do you deliver to Pune?”)
LiveChatBot.setVisitor({ name: “Asha”, email: “asha@example.com” })
LiveChatBot.on(“lead”, function () { … })

⚡ In short

  • The install script creates window.LiveChatBot, which your own HTML and JavaScript can call.
  • Because the script loads asynchronously, wrap calls in a tiny helper that waits for the chat to be ready.
  • Use data attributes on your buttons, so one helper handles every “open chat” and “ask a question” button.
  • setVisitor needs an email or phone; only pass details for logged-in customers.
Add chatbot to website html in LiveChatBot: AI Agent knowledge sources and a sample chat answered from them
How it looks in LiveChatBot: AI Agent knowledge sources and a sample chat answered from them.

Why your own buttons

When the bubble isn’t enough

The floating bubble works everywhere, but it’s generic and easy to ignore. Buttons in your own page do three things the bubble can’t.

They sit where the question happens

“Ask about this product” next to the price, “Questions about fees?” under the fee table, “Need help choosing?” on a comparison page. A visitor who has a question at that exact spot sees an invitation right there.

They can start the conversation for the visitor

A button can send a ready-made question, such as “Do you deliver to my pin code?”, so the visitor doesn’t have to type. Your bot or team answers straight away. That’s especially useful on phones, where typing is the biggest barrier.

They fit your design

A text link in the menu, a button in your brand’s style, a line in the footer. The chat opens from wherever your design puts it.

All of this uses the JavaScript API that comes with the standard install, with no extra plugin. If you haven’t installed the chat yet, see our HTML install guide first; this guide assumes the one-line script is already on your pages.

The API

What the LiveChatBot JavaScript API can do

Available as window.LiveChatBot on any page with the install script.

CallWhat it does
open() / close() / toggle()Opens, closes or flips the chat window.
show() / hide()Shows or removes the chat on the current page, for example hiding it during a video or on a step of a form.
isOpen()Returns true when the chat window is open.
sendMessage(text)Opens the chat and sends the text as the visitor’s message.
setVisitor({ name, email, phone })Tells your team who the visitor is. Needs an email or a phone number.
pageView()Reports a page change in single-page apps, so your team sees the right page.
on(event, fn) / off(event, fn)Runs your code on ready, open, close, message, messageSent or lead.

For anything beyond these, ask our team for developer documentation. The REST API, webhooks and Zapier, which work on the server side, are listed on the integrations page.

Free tool

Add chatbot to website HTML: build and try your buttons

Tick what you need. The builder writes the helper, the buttons and the optional extras, then lets you click the buttons against a pretend chat.

Tick what you need and edit the words. The builder writes the code, then lets you try your buttons here against a pretend chat that shows which call each click makes. Nothing leaves your browser.

The timing trap

Why custom chat buttons sometimes do nothing

The install script has the async attribute, so it loads in the background while your page carries on. That's good for speed, but it means window.LiveChatBot doesn't exist for the first moments after the page appears. A button that calls LiveChatBot.open() directly works when you test it slowly and fails for the visitor who taps it the instant the page loads. In the console, you'd see "LiveChatBot is not defined".

The fix: a tiny helper

The builder's helper, lcb(), checks whether the chat is there. If it is, it runs your code straight away. If not, it waits for the "livechatbot:ready" event, which the widget fires on the window once it's ready, and runs your code then. Once the script has loaded, calls made before the chat finishes starting up are queued and run in order, so you don't need to handle that case yourself.

One listener for every button

Instead of an onclick on each button, the helper listens for clicks on anything with a data-chat attribute. Add data-chat to open the chat, or data-chat plus data-chat-message to send a question. New buttons need no extra JavaScript, and buttons added later by your page builder work too.

Order on the page

Paste the helper after the install script, near the end of the body. Buttons can be anywhere.

Ask-a-question buttons

Writing questions that start good conversations

sendMessage opens the chat and sends your text as the visitor's own first message. Your bot or team sees it and replies, exactly as if the visitor had typed it. That makes it powerful, and worth writing carefully.

Write in the visitor's voice

"Do you deliver to my pin code?" not "Delivery information". It appears as their message, so it should sound like them.

Make it specific to the page

On a product page, include the product: "Is the teak study table available in walnut?" If your pages are generated from a template, the product name can be inserted by your template.

Make sure something answers it

Before you add the button, check that a saved reply or your AI knowledge covers that question, or that someone is online to answer. A button that sends a question nobody answers is worse than no button. See how to use saved replies.

Don't overdo it

One or two question buttons per page is plenty. If you need many, a single "Chat with us" button that opens the chat to a welcome message is cleaner.

When you add chatbot to website HTML pages this way, the visitor's first message arrives already focused, and your answer can be too.

Logged-in customers

Passing a name and email safely

If your site has accounts, setVisitor tells your team who they're talking to, so nobody has to ask "can I have your email?". It needs at least an email or a phone number; a name alone is ignored.

Print the values from your server

The builder shows patterns for PHP and Shopify Liquid. In each, your server writes the logged-in customer's details into the page as properly escaped JavaScript strings (json_encode with the JSON_HEX flags in PHP, the json filter in Liquid). Never paste raw values into a script without escaping: a name with a quote mark would break the code, and unescaped values are a security risk.

Only for logged-in visitors

Wrap the code in your template's "if logged in" condition. Visitors who aren't signed in shouldn't get anyone's details.

Pass only what helps

Name and email, or name and phone, are enough for most businesses. Don't pass order histories, addresses or anything sensitive through the page.

Say so in your privacy notice

Your privacy policy should mention that chat conversations are linked to customer accounts when people are signed in. Document which fields you share and why, and ask qualified advisers about the privacy requirements that apply to your business.

Example

A product page with two buttons

Illustrative HTML. The install script and helper sit at the end of the body.

<div class="price">Rs 12,499</div>
<button type="button" data-chat
  data-chat-message="Is the teak study table available in walnut?">
  Ask about colours
</button>
<a href="/contact" data-chat>Chat with us</a>

<!-- end of body -->
<script src="https://livechatbot.in/widget.js" data-key="YOUR_KEY" async></script>
<script>
function lcb(fn) {
  if (window.LiveChatBot) { fn(window.LiveChatBot); }
  else { window.addEventListener("livechatbot:ready", function () { fn(window.LiveChatBot); }); }
}
document.addEventListener("click", function (e) {
  var b = e.target.closest("[data-chat]");
  if (!b) return;
  e.preventDefault();
  var msg = b.getAttribute("data-chat-message");
  lcb(function (chat) { if (msg) { chat.sendMessage(msg); } else { chat.open(); } });
});
</script>

The link version keeps working as a normal contact link if JavaScript is off or the chat is blocked, while visitors with the chat get it opened instead. Note that if the chat script can't load at all, for example because of an ad blocker, the click is still captured; for important links, consider removing preventDefault so the link falls back to its page.

Events and analytics

Reacting to what happens in the chat

on() lets your page react to the chat. The events are ready, open, close, message (a reply arrived), messageSent (the visitor sent something) and lead (the visitor left an email or phone number).

Count leads in Google Tag Manager

The builder's lead option pushes a chat_lead event into the dataLayer when a visitor leaves their details. In Tag Manager, create a trigger on that event and send it to your analytics or ads tags as a conversion.

Pause other pop-ups while the chat is open

If your site shows a newsletter pop-up, skip it when the chat is open: check LiveChatBot.isOpen() before showing it, or set a flag on the open and close events.

Single-page apps

In React, Vue or similar apps, the page doesn't reload between screens. Call pageView() after each route change so your team's visitor view follows the visitor. LiveChatBot also has React and Vue plugins.

To add chatbot to website HTML pages with these extras, test each one in a private window with the browser console open, so any error shows up immediately.

Test

Before you ship your buttons

Tick each one. Your ticks stay in this browser tab only.

Button test

Frequently asked questions

How do I open the chat from my own button?

Call LiveChatBot.open(), wrapped in a helper that waits for the chat to load, or give the button a data-chat attribute and use the helper from the builder above.

Why does my chat button work only sometimes?

The install script loads asynchronously, so window.LiveChatBot may not exist yet when the visitor clicks. Wait for the livechatbot:ready event before calling it.

Can a button send a question for the visitor?

Yes. sendMessage(text) opens the chat and sends the text as the visitor's message. Make sure a saved reply, AI knowledge or a person can answer it.

How do I add chatbot to website HTML with logged-in names?

Use setVisitor with the customer's name and email, printed by your server as escaped JavaScript, only when the customer is signed in.

Is there a React or Vue version?

Yes. LiveChatBot has React and Vue plugins; ask our team for developer details.

Keep reading

Related guides

📘

Add Chatbot to Website in Stages: A Soft-Launch Plan That Tests Your Bot on Safe Pages First

Read the guide →

🧭

Add Chat Widget to Website With Cards People Tap: WhatsApp, Call, Email and Link Cards, Planned and Previewed

Read the guide →

🛠️

Add Chat to Your Website Without Drowning in It: A Workload Estimator for Small Teams

Read the guide →

Make the chat part of your page

Install the script, add the helper, and give your visitors buttons that ask the right question. Start free on livechatbot.in.