Skip to content

Instantly share code, notes, and snippets.

@DeskWOW
Last active August 29, 2015 13:59
Show Gist options
  • Save DeskWOW/10641882 to your computer and use it in GitHub Desktop.
Save DeskWOW/10641882 to your computer and use it in GitHub Desktop.
Persisting Chat — The following is a custom Desk.com chat widget that supports persisting chat session as user navigates pages. [Demo: https://zzz-ty.desk.com/?t=444102]
<!-- At the top of this theme, place the following code: -->
<script>document.cookie="chat_id=;expires=Thu, 01 Jan 1970 00:00:01 GMT;path=/";</script>
<!-- At the top of this theme, place the following code: -->
<script>document.cookie="chat_id=" + window.location.href.split('/').pop() + "; path=/";</script>
<!-- Where you want your chat to appear/persist, place the following code: -->
<style>
.chat-link-container {
display: none;
}
a.chat-link {
display: inline-block;
box-sizing: border-box;
background: red;
color: white;
padding: 10px 20px;
margin: 10px;
}
a.chat-link:hover {
text-decoration: none;
}
.chat-container {
display: none;
padding: 0;
height: 350px;
overflow: hidden;
}
.chat {
width: 100%;
height: 100%;
}
</style>
<div class="chat-container">
<iframe class="chat" frameborder="0"></iframe>
</div>
<div class="chat-link-container">
<a class="chat-link" href="#">Start chat</a>
</div>
<hr>
<script>
function getCookie(name) {
match = $("iframe.chat")[0].contentWindow.document.cookie.match(new RegExp(name + '=([^;]+)'));
if (match) return match[1];
}
$(function() {
chat_id = getCookie("chat_id");
console.log(chat_id);
if (chat_id) {
$(".chat").prop("src", "https://zzz-ty.desk.com/customer/widget/chats/" + chat_id);
setTimeout(function() {
chat_id = getCookie("chat_id");
console.log(chat_id);
if (chat_id) {
$(".chat-container").show();
} else {
$(".chat-link-container").show();
}
}, 1000);
} else {
$(".chat-link-container").show();
}
$(".chat-link").click(function() {
$(".chat-link-container").hide();
$(".chat-container").show();
chat_id = getCookie("chat_id");
if (chat_id) {
$(".chat").prop("src", "https://zzz-ty.desk.com/customer/widget/chats/" + chat_id);
} else {
$(".chat").prop("src", "https://zzz-ty.desk.com/customer/widget/chats/new");
}
return false;
});
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment