Home > Technical Support > LiveAgent API > JavaScript API > Button.onOnline(), Button.onOffline()

Button.onOnline(), Button.onOffline()

Sometimes you might need to execute some custom javascript code just after a chat button is loaded depending on whether the button is online or offline. For this situation, we added these 2 callback functions in version 4.17.3 which you can override and specify your own code that will execute.
Button.onOnline()
Button.onOffline()
 

Example use

For example, let's say that we want to display chat button if there are available agents, but we might not want to show the offline button, but show the knowledge base search widget instead. To do this, we first need to set the chat button to not display the offline variant. Simply uncheck the option Display offline button when chat is unavailable.
 
Then we will use the integration code of the button as usual and do some modifications. This is the unchanged integration code:
<script type="text/javascript">
(function(d, src, c) { var t=d.scripts[d.scripts.length - 1],s=d.createElement('script');s.id='la_x2s6df8d';s.async=true;s.src=src;s.onload=s.onreadystatechange=function(){var rs=this.readyState;if(rs&&(rs!='complete')&&(rs!='loaded')){return;}c(this);};t.parentElement.insertBefore(s,t.nextSibling);})(document,
'//localhost.lc/LiveAgent/LiveAgent/server/scripts/track.js',
function(e){ LiveAgent.createButton('70735e08', e); });
</script>
We change the call
LiveAgent.createButton('70735e08', e);
to this:
var button1 = LiveAgent.createButton(' 70735e08 ', e); 
button1.onOffline = function() {
  // code to execute when button is offline >>>
  (function(d, src, c) { var t=d.scripts[d.scripts.length - 1],s=d.createElement('script'); s.id='la_x2s6df8d';s.async=true;s.src=src; 
    s.onload=s.onreadystatechange=function(){var rs=this.readyState;if(rs&&(rs!='complete')&&(rs!='loaded')){return;}c(this);};t.parentElement.insertBefore(s,t.nextSibling);})
    (document, '//localhost.lc/LiveAgent/LiveAgent/server/scripts/track.js',
    function(e){ LiveAgent.createKbSearchWidget('b3e5a253', e); });
  // <<< end code
  };

Explanation

The changed code assigns the Button object to variable button1, and then the button1.onOffline is overridden with my own custom function. This function will be executed if the button is offline.
Notice that in the function I used the complete integration code of the knowledge base search widget (except for the opening tag <script> and the closing tag </script>).
 
 

Ready to try LiveAgent?

It's free for the first 14 days! No credit card required.

Get Started
We work well with others...
Magento Joomla Wordpress Mailchimp
Contact us

support@liveagent.com

+1-800-811-6590 (Toll Free in USA & Canada)

+421 2 33 456 826 (European Union & Worldwide)

Quality Unit, LLC 616 Corporate Way, Suite 2-3278 Valley Cottage, NY 10989

Stay in touch
Blog Google+
© 2004-2020 LiveAgent.com, All rights reserved