Monday, January 10, 2011

Tiện ích Nhận xét mới nhất theo kiểu cơ bản

Tôi đã giới thiệu kiểu tiện ích Nhận xét mới nhất có ảnh đại diện và tiện ích Nhận xét mới nhất có icon đại diện. Theo yêu cầu của bạn Linkfoci, hôm nay tôi xin giới thiệu kiểu tiện ích Nhận xét mới nhất cơ bản. Tiện ích này hiển thị phần tác giả nhận xét, ngày đăng nhận xét và tiêu đề nhận xét.



Bạn có thể xem Demo dưới đây.

Nhận xét mới nhất




Để cài đặt tiện ích này, bạn chỉ cần đặt đoạn code sau đây vào một tiện ích HTML/JavaScript là được.

<script style="text/javascript">
function showrecentcomments(json) {
var numcomments = 5; // Bạn có thể thay đổi số 5 để hiển thị số nhận xét
var showcommentdate = true; // Đổi true thành false để ẩn ngày đăng nhận xét
var showposttitle = true; // Đổi true thành false để ẩn tiêu đề bài viết
var numchars = 100; // Đổi số 100 để hiển thị số ký tự nhận xét
for (var i = 0; i < numcomments; i++) {
var entry = json.feed.entry[i];
var alturl;
if (i == json.feed.entry.length) break;
for (var k = 0; k < entry.link.length; k++) {
if (entry.link[k].rel == 'alternate') {
alturl = entry.link[k].href;
break;
}
}
alturl = alturl.replace("#", "#comment-");
var postlink = alturl.split("#");
postlink = postlink[0];
var linktext = postlink.split("/");
linktext = linktext[5];
linktext = linktext.split(".html");
linktext = linktext[0];
var posttitle = linktext.replace(/-/g," ");
posttitle = posttitle.link(postlink);
var commentdate = entry.published.$t;
var cdyear = commentdate.substring(0,4);
var cdmonth = commentdate.substring(5,7);
var cdday = commentdate.substring(8,10);
var monthnames = new Array();
monthnames[1] = "tháng 01"; // Đổi tháng 01 thành Jan hoặc January nếu blog bạn bằng tiếng Anh
monthnames[2] = "tháng 02"; // Feb hoặc February
monthnames[3] = "tháng 03"; // Mar hoặc March
monthnames[4] = "tháng 04"; // Apr hoặc April
monthnames[5] = "tháng 05"; // May
monthnames[6] = "tháng 06"; // Jun hoặc June
monthnames[7] = "tháng 07"; // Jul hoặc July
monthnames[8] = "tháng 08"; // Aug hoặc August
monthnames[9] = "tháng 09"; // Sept hoặc September
monthnames[10] = "tháng 10"; // Oct hoặc October
monthnames[11] = "tháng 11"; // Nov hoặc November
monthnames[12] = "tháng 12"; // Dec hoặc December
if ("content" in entry) {
var comment = entry.content.$t;}
else
if ("summary" in entry) {
var comment = entry.summary.$t;}
else var comment = "";
var re = /<S[^>]*>/g;
comment = comment.replace(re, "");
document.write('<br/>');
if (showcommentdate == true) document.write('Vào ngày ' + ' ' + cdday + ' ' + monthnames[parseInt(cdmonth,10)] + ' năm ' + ' ' + cdyear + ' ');
document.write('<a href="' + alturl + '">' + entry.author[0].name.$t + '</a> đã bình luận');
if (showposttitle == true) document.write(' về bài viết ' + posttitle);
document.write('<br/>');
if (comment.length < numchars) {
document.write('<i>');
document.write(comment);
document.write('</i>');}
else
{
document.write('<i>');
comment = comment.substring(0, numchars);
var quoteEnd = comment.lastIndexOf(" ");
comment = comment.substring(0, quoteEnd);
document.write(comment + '...<a href="' + alturl + '">(Xem thêm …)</a>');
document.write('</i>');}
document.write('<br/>');
}
document.write('<br/>');
}
</script>
<script src="http://huynh-nhat-ha.blogspot.com/feeds/comments/default?alt=json-in-script&callback=showrecentcomments">
</script>
<div class='clear'></div>

Bạn có thể thay đổi những phần được đánh dấu màu đỏ và màu xanh ở trên. Thay huynh-nhat-ha thành tên blogspot của bạn.

Kiểu tiện ích này thích hợp cho blogspot bằng tiếng Anh.

No comments:

Post a Comment