[SalesForce] How to redirect to pre-chat when liveagent available but manually click liveagent button

When open live agent sandbox page it will :

  1. detect liveagent.
  2. show/hide pre-chat button by liveagent is available or not.

so I have to manually click the button then start the prechat page.
However what I need is :

  1. detect liveagent
  2. Auto redirect to prechat page when there is liveagent

Currently I handle it by detect the button attribute change and use javascript to trigger its click, but I think it is a sneak way.

Is there any sample / api document to solve this?
Really appreciate any hint or idea, thanks first.

Hint live_agent_sandbox_page.html

<html>
  <body>  
        <script type='text/javascript' src='https://c.la9cs.salesforceliveagent.com/content/g/js/34.0/deployment.js'></script>
        <script type='text/javascript'>
            liveagent.init('https://d.la9cs.salesforceliveagent.com/chat', 'DeploymentId', 'Org_Id');
        </script>   
        <button id="liveagent_button_online_buttonID" href="javascript://Chat" style="display: none;" onclick="liveagent.startChat('buttonID')">
            Online Chat Content 
        </button>
        <div id="liveagent_button_offline_buttonID" style="display: none;">
            Offline Chat Content
        </div>
        <script type="text/javascript">
            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'));
            });
        </script>
  </body>
</html>

Best Answer

You can use the event callback for the chat button. The callback function will automatically be called whenever status of the chat button changes. You would still need to use javascript to trigger the button click.

function btnEventHandler(e){
    if(e == liveagent.BUTTON_EVENT.BUTTON_AVAILABLE){
        alert("Available!");
    }else if(e == liveagent.BUTTON_EVENT.BUTTON_UNAVAILABLE){
        alert("Gone!");
    }
}

liveagent.addButtonEventHandler('buttonID', btnEventHandler);


liveagent.init('https://d.la2c1.salesforceliveagent.com/chat', 'DeploymentId', 'Org_Id');