Developer: How to automatically open the chat (jQuery)

Signup for a free 18-days trial today

Developer: How to automatically open the chat (jQuery)

In Knowledge base >

Note: This is a jQuery example.

This is an example of how to automatically open the chat window after a number of seconds. It also remembers the user so it does not happen on all pages the user loads. Only the first time.

You should use this in cases where you want more activity from your users, by showing them the possibility of direct chatting with you. You can setup many reactions in Settings under the backend administration interface, e.g. auto start the chat session, don’t show the chat when you are offline, and more.

The 30*1000 is the number of milliseconds, which correspond to 30 seconds. It sets a timeout when the page DOM has been loaded, and when the timeout is reached it calls openChat().

The openChat() function will open the chat only if the chat has been loaded, and the cookie has not yet been set.

<script type="text/javascript">
jQuery(document).ready(function(){
	var timeout_open_chat = setTimeout('openChat()', 30*1000);
});

function openChat()
{
	if (typeof cs_openchat == "function" && getCookie('chat_opened') != 'true')
	{
		cs_openchat();
		setCookie('chat_opened', 'true', null);
	}
}

function setCookie(c_name,value,exdays)
{
	var exdate=new Date();
	exdate.setDate(exdate.getDate() + exdays);
	var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
	document.cookie=c_name + "=" + c_value;
}

function getCookie(c_name)
{
	var i,x,y,ARRcookies=document.cookie.split(";");
	for (i=0;i<ARRcookies.length;i++)
	{
		x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
		y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
		x=x.replace(/^\s+|\s+$/g,"");
		if (x==c_name)
		{
			return unescape(y);
		}
	}
}
</script>

Note: Cookie functions are just standard W3School examples, taken from http://www.w3schools.com/js/js_cookies.asp