Sunday, 6 November 2011

Membuat Recent Comment dengan Javascript

Mempercantik blog dengan javascript terutama untuk membuat recent comment pada blog bisa anda lakukan dengan mengikuti langkah berikut. Silahkan anda copy kode berikut ini dan letakkan setelah kode <body>  pada template anda
<script style='text/javascript'>
function showrecentcomments(json) {
for (var i = 0; i &lt; 6; i++) {
var entry = json.feed.entry[i];
var ctlink;
if (i == json.feed.entry.length) break;
for (var k = 0; k &lt; entry.link.length; k++) {
if (entry.link[k].rel == &#39;alternate&#39;) {
ctlink = entry.link[k].href;
break;
}
}
ctlink = ctlink.replace(&quot;#&quot;, &quot;#comment-&quot;);
var ptlink = ctlink.split(&quot;#&quot;);
ptlink = ptlink[0];
var txtlink = ptlink.split(&quot;/&quot;);
txtlink = txtlink[5];
txtlink = txtlink.split(&quot;.html&quot;);
txtlink = txtlink[0];
var pttitle = txtlink.replace(/-/g,&quot; &quot;);
pttitle = pttitle.link(ptlink);
if (&quot;content&quot; in entry) {
var comment = entry.content.$t;}
else
if (&quot;summary&quot; in entry) {
var comment = entry.summary.$t;}
else var comment = &quot;&quot;;
var re = /&lt;S[^&gt;]*&gt;&gt;/g;
comment = comment.replace(re, &quot;&quot;);
if (i==0) {
document.write(&#39;&lt;li class=&quot;clear&quot; style=&quot;border-top: 0pt none;&quot;&gt;&#39;);
}
else{
document.write(&#39;&lt;li class=&quot;clear&quot;&gt;&#39;);
}
document.write(&#39;&lt;a href=&quot;&#39; + ctlink + &#39;&quot;&gt;&lt;strong&gt;&#39; + entry.author[0].name.$t + &#39;&lt;/strong&gt;&lt;/a&gt; says:&#39;);
if (comment.length &lt; 100) {
document.write(&#39;&lt;a target=&quot;_blank&quot; href=&quot;&#39; + ctlink + &#39;&quot;&gt;&#39;+comment+ &#39;&lt;/a&gt;&#39;);
}
else
{
comment = comment.substring(0, 100);
var quoteEnd = comment.lastIndexOf(&quot; &quot;);
comment = comment.substring(0, quoteEnd);
document.write(&#39;&lt;a target=&quot;_blank&quot; href=&quot;&#39; + ctlink + &#39;&quot;&gt;&#39;+comment + &#39;...&lt;/a&gt;&#39;);
}
}
document.write(&#39;&lt;/li&gt;&#39;);
}
</script>

Kemudian anda tambahkan element baru html/javascript dan berikan title recent comment atau apa saja terserah anda, lalu masukkan kode berikut ini
<ul id='recentcomments'>
<script src='/feeds/comments/default?alt=json-in-script&amp;callback=showrecentcomments'/>
</ul></script></ul>

Selamat mencoba

Share this:

Related Posts
Disqus Comments