2

I am trying to setup liveagent but am running into an issue with starting the chat.

We have a form that the user enters information about themselves, and then a button to begin the chat.

I use the information entered to add Custom Details to the console and init liveagent. Once this has happened I then call liveagent.startChat().

This call is throwing an error

Uncaught TypeError: Cannot read property 'startChat' of undefined

This is most likely because liveagent.init() hasn't completed, but liveagent doesn't return a value and has no callback function.

How can I wait until liveagent.init() has complete without manually setting a timeout?

Chat button

<a href="#" class="fona-button" id="liveagent_button_online_{!buttonId}" onclick="chat()">Chat With Us</a>

onclick function

function chat() {
        var firstName = $('#firstName').val();
        var lastName = $('#lastName').val();
        var email = $('#email').val();
        var phone = $('#phone').val();
        var endApp = $('#endApp').val();

        var cleanPhone = '';

        if (!firstName || !lastName || !email || !phone || !endApp) {
            alert('Please fill all form fields');
        } else {
            cleanPhone = phone.match(/\d+/g)[0];
            liveagent.setName(firstName + ' ' + lastName);
            liveagent.addCustomDetail('FirstName', firstName);
            liveagent.addCustomDetail('LastName', lastName);
            liveagent.addCustomDetail('Email', email);
            liveagent.addCustomDetail('Phone', phone);
            liveagent.addCustomDetail('End Application', endApp);
            liveagent.findOrCreate('Contact')
                .map('LastName', 'LastName', true, true, false)
                .map('Phone', 'Phone', true, true, false)
                .map('Email', 'Email', true, true, false);
            liveagent.init('{!url}/chat', '{!deploymentId}', '{!organizationId}');
            if (!window._laq) { 
                window._laq = []; 
            }
            window._laq.push(function(){
                liveagent.showWhenOnline('{!buttonId}', document.getElementById('liveagent_button_online_{!buttonId}'));
                liveagent.showWhenOffline('{!buttonId}', document.getElementById('liveagent_button_offline_{!buttonId}'));
            });
            liveagent.startChat('{!buttonId}');
        }
    }
0

To be honest, every time I got that error was because the method

liveagent.showWhenOnline('YOUR_ID_HERE', document.getElementById('your_element')); 

was using one ID and then, the

onclick="liveagent.startChat('YOUR_ID_HERE')" 

was using a different ID. Try to make sure you have the same ID on both places.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.