Change button on demand without page reload

Sometimes you need "refresh" chat button without page reloading, for example in case of using angularJS or dynamic language switching.

Use button integration code without `LiveAgent.createButton`, so your code will look like:

<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,
'//yourdomain.com/scripts/track.js',
   function(e){});
</script> 

Then you can display chat button by this way:

document.body.innerHTML += '<div id="chatButton"></div>';
chatButton = LiveAgent.createButton('BUTTON_ID', document.getElementById("chatButton"));

And destroy by:

if(!LiveAgent.instance.openedWidget) {
    if(document.getElementById(chatButton.elementId)) {
        document.getElementById(chatButton.elementId).parentNode.removeChild(document.getElementById(chatButton.elementId));
    }
    if(document.getElementById("ic" + chatButton.elementId)) {
        document.getElementById("ic" + chatButton.elementId).parentNode.removeChild(document.getElementById("ic" + chatButton.elementId));
    }
    if(document.getElementById("if" + chatButton.elementId)) {
        document.getElementById("if" + chatButton.elementId).parentNode.removeChild(document.getElementById("if" + chatButton.elementId));
    }
    LiveAgent.instance.elements = [];
    LiveAgent.instance.widgets = [];
}

 

×