• Đăng ký
  • Đăng nhập

Code hiển thị lượt xem bài viết đơn giản cho Blogspot

📅 — 👀 1374 — 👦

Trang này có hữu ích không?

👍 Có (16)        👎 Không (18)


Lướt web vô tình phát hiện được cách hiển thị số lượng xem bài viết hay hay, nên lấy về áp dụng cho Blogspot. Khách quan ban đầu test thì thấy load nhanh (chắc do lấy số liệu từ google - https://console.firebase.google.com/).


Chú ý: Chỉ nên dùng hiển thị số bài viết trong trang item, vì nếu bạn lạm dụng nhiều quá có thể gây tình trạng website load chậm.

duy<!-- Code by THichCode.NET -->
<!-- Code có dùng JQuery -->
<b:if cond='data:blog.pageType == "item"'>
<style>
.viewers .loading {
display: inline-block;
width: 20px;
height: 20px;
background: transparent url('http://2.bp.blogspot.com/-60aISBQLRY0/VFBMqzLfzGI/AAAAAAAABgM/GInmVTvEzkc/s1600/loader.gif') no-repeat 0 0;
vertical-align: middle
}
</style>

<!-- Bạn đặt thẻ này tại vị trí cần hiển thị số lượt xem là ok -->
<span class='viewers label-info' expr:id='data:post.id'> <span class='loading'></span> lượt xem</span>

<script src='http://cdn.firebase.com/v0/firebase.js' type='text/javascript'></script>
<script type='text/javascript'>
//<![CDATA[
// Hàm xử lý số lượt xem
$(function() {
var a = $(".viewers");
a.each(function() {
var n = $(this).attr("id"),
/*
đây là project đã được tạo sẵn, nếu bạn không thích thì 
tạo mới tại https://console.firebase.google.com/ 
*/
e = new Firebase("project-1915249200075701329.firebaseio.com/" + n);
e.once("value", function(n) {
var i = n.val();
null == i ? i = 1 : "/" != window.location.pathname && i++, e.set(i), a.children("span").removeClass("loading").html(i)
})
})
});
//]]>
</script>
</b:if>

Trả lời


📁 Blogspot, Code Demo