Skip to content

Instantly share code, notes, and snippets.

@skipjac
Created May 8, 2012 22:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save skipjac/2639857 to your computer and use it in GitHub Desktop.
Save skipjac/2639857 to your computer and use it in GitHub Desktop.
Zendesk forums JSONP callback
<!doctype html>
<head>
<title>My site</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
</head>
<body>
<p>Latest announcements on support.zendesk.com:</p>
<div id="announcements"></div>
<script type="text/javascript">
var zendesk_url = 'https://support.zendesk.com';
var forum_id = 1;
var max_number_of_items = 10;
var months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
truncate = function(text, length, ellipsis) {
// Set length and ellipsis to defaults if not defined
if (typeof length == 'undefined') var length = 100;
if (typeof ellipsis == 'undefined') var ellipsis = '...';
// Return if the text is already lower than the cutoff
if (text.length < length) return text;
// Otherwise, check if the last character is a space.
// If not, keep counting down from the last character
// until we find a character that is a space
for (var i = length-1; text.charAt(i) != ' '; i--) {
length--;
}
// The for() loop ends when it finds a space, and the length var
// has been updated so it doesn't cut in the middle of a word.
return text.substr(0, length) + ellipsis;
}
$(function() {
$.getJSON(zendesk_url + '/forums/' + forum_id + '/entries.json?callback=?', function(entries) {
for (var i = 0; i < max_number_of_items; i++) {
var entry = entries[i];
var itemDate = new Date(entry.created_at);
var bodyTrimmed = $.trim(entry.body.replace(/(<.*?>)/ig,""));
var newBody = truncate(bodyTrimmed, 200, '...');
$("#announcements").append('<p><b>' + months[itemDate.getMonth()] + ' ' + itemDate.getDate() + ': '
+ '<a href="' + zendesk_url + '/entries/' + entry.id + '" target="_blank">' + entry.title + '</a></b><br/>'
+ newBody
+ '<a href="' + zendesk_url + '/entries/' + entry.id + '" target="_blank"> read more</a></p>');
}
});
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment