Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

External Javascript on click loading then using its API

So been looking for a good chat program that has a windows app. Found Tawk I pretty large app so I wanted to load it only if someone really wanted to chat with me.

The problem is once you click my chat button, the chat box is in a minimized state. I would like the widget to maximize once its loaded. They have an api to maximize.

Tawk_API.onLoad = function(){
    Tawk_API.maximize();
};

Unfortunately, this only works when the program is fully loaded.

I tried a timer function that looks for a class but they change there class names on each load class="KSKLwOL-1492537819846".

Hope there might be another approach to know when the program is loaded then maximize.

$('.chat').on('click', function (e) {
    $('.chat').hide();
    var Tawk_API = Tawk_API || {},
        Tawk_LoadStart = new Date();
    (function () {
        var s1 = document.createElement("script"),
            s0 = document.getElementsByTagName("script")[0];
        s1.async = true;
        s1.src = 'https://embed.tawk.to/58f11df330ab263079b5fde1/default';
        s1.charset = 'UTF-8';
        s1.setAttribute('crossorigin', '*');
        s0.parentNode.insertBefore(s1, s0);
        document.body.appendChild(s0);
    })();
}); 

The chat program is loaded on my site if you want to see what I mean.

Thanks, anybody.

like image 607
rgfx Avatar asked Sep 21 '26 08:09

rgfx


1 Answers

One way is to load API and hide widget as default using

Tawk_API.onLoad = function(){
  Tawk_API.hideWidget();
};

and on your button click

$('.chat').on('click', function (e) {
$('.chat').hide();
Tawk_API.showWidget();

Update:

You are currently loading the Tawk api on your chat button click. I suggest you should load it on your page as Tawk recommended. Put your script before the </body> tag.

This will save you the hassle of timer or waiting for the API to load. In result, when you DOM is ready(fully loaded), you will have Tawk api object

HTML

<body>
    <! -- move your code given by Tawk here -->
</body>

Javascript

$(document).ready(function(){
    if (Tawk_API) {
     Tawk_API.onLoad = function() {
       Tawk_API.hideWidget();
     };
     $('.chat-button').on('click', function(){
       $('.chat-button').hide();
       Tawk_API.showWidget();
       Tawk_API.maximize();
     });
    }
 });

Still, if this is not understandable, Here is a codepen demonstration of what I meant to say . Check this link

like image 120
Farooq Ahmed Khan Avatar answered Sep 23 '26 22:09

Farooq Ahmed Khan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!