2009-02-24

Blogger 列出 [最新回應] 的 JavaScript 語法

我加入一個 typeof 的判斷
避免如果 Blogger 一個回應也沒有時 (像我) 會發生錯誤

出處在這裡




<div id="newComments">
<p align="center"><img src="http://dino9021.myweb.hinet.net/BlogJS/loading.gif"/></p><p align="center">Loading....</p>
<noscript>failed!<br/>Javascript not supported here!</noscript>
</div>

<script>

function handleComments(json) {

function compareentry(a,b) {
order= Date.parse(a.published.$t.replace(/^(\d{4})-(\d{2})-(\d{2})T([0-9:]*)([.0-9]*)(.)(.*)$/, '$1/$2/$3 $4 GMT')) - Date.parse(b.published.$t.replace(/^(\d{4})-(\d{2})-(\d{2})T([0-9:]*)([.0-9]*)(.)(.*)$/, '$1/$2/$3 $4 GMT'));
return 0-order;
};
var temp = '<ul>';
var CommentShow = 10;
if (typeof(json.feed.entry)=='undefined') {
document.getElementById("newComments").innerHTML="<ul>None...</ul>";
return;
};
var sortentry = json.feed.entry.sort(compareentry);

for (var i=0, Comment; Comment = sortentry[i]; i++) {

if (i >= CommentShow) {
break;
};
var title=Comment.content.$t.substr(0,30);
title=title.replace(new RegExp("BR","gm"), "");
title=title.replace(new RegExp("</>","gm"), " ");
var link=Comment.link[2].href.replace("#", "#");
temp += '<li><span class="item-title"><a href="'+link+'">'+ title +'</a></span></li>';
};
temp+="</ul>";
document.getElementById("newComments").innerHTML = temp;

};

</script>

<script src="/feeds/comments/default?alt=json-in-script&callback=handleComments" type="text/javascript"/></script>


把 Loading... 的地方改成以下 Code 就有 Loading 的動畫圖示
<p align="center"><img src="http://dino9021.myweb.hinet.net/BlogJS/loading.gif"/></p><p align="center">Loading....</p>

1 則留言: