Basic - Recent Posts with Summary

Unlike others, summary is used as feed parameter instead of default. Because entry.summary.$t cannot work if there isn't jump-break in post and it will show all post content. There is other problem about this. Probably it's about html tags, An unbalanced tree was written using document.write() causing data from the network to be reparsed for firefox 4+ Maybe we can solve this problem with a function like removeHTMLtag.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051<script>
var numposts = 4;
var isList = true;

function removeHtmlTag(summary,len_summary){
    if(summary.indexOf("<")!=-1)
    {
        var s = summary.split("<");
        for(var i=0;i<s.length;i++){
            if(s[i].indexOf(">")!=-1){
                s[i] = s[i].substring(s[i].indexOf(">")+1,s[i].length);
            }
        }
        summary = s.join("");
    }
    len_summary = (len_summary < summary.length-1) ? len_summary : summary.length-2;
    while(summary.charAt(len_summary-1)!=' ' && summary.indexOf(' ',len_summary)!=-1) len_summary++;
    summary = summary.substring(0,len_summary-1);
    return summary+'...';
}

function showrecentposts(json) {
    for (var i = 0; i < 4; i++) {
        var entry = json.feed.entry[i];
        var postTitle = entry.title.$t;
        var postSummary = entry.summary.$t;
        
        postSummary = removeHtmlTag(postSummary, 300);
        
        var postUrl;
        
        if (i == json.feed.entry.length) break;
        for (var k = 0; k < entry.link.length; k++) {
            if (entry.link[k].rel == "alternate") {
                postUrl = entry.link[k].href;
                break;
            }
        }
        postTitle = postTitle.link(postUrl);
        if (isList) document.write("<li>");
        document.write(postTitle + postSummary);
    }
    if (isList) document.write("</li>");
}

</script>

<ul>
<script src="//btdocumentation.blogspot.com/feeds/posts/summary?max-results=900&amp;alt=json-in-script&amp;callback=showrecentposts">
</script>
</ul>

  • Basic - Recent Posts with LabelWe can pull the post's first label with entry.category[0].term;. There is no need to get more labels for recent posts. But if we don't add any label on the posts, probably it won't work regularly. The ternary operator has been used to manipulate this problem. <script> var numposts = 4; var isList...
  • Basic - Recent Posts with SummaryUnlike others, summary is used as feed parameter instead of default. Because entry.summary.$t cannot work if there isn't jump-break in post and it will show all post content. There is other problem about this. Probably it's about html tags, An unbalanced tree was written using document.write() causing...
  • Basic - Recent Posts with ThumbnailsTo get the thumbnail, we can use entry.media$thumbnail. However, this isn't enough for it to working without errors. Some posts may not contain images. This situation will lead to an error. entry.content.$ is available to solve the problem. This gives content with html. In this the content variable,...
  • Basic - Recent Posts [Yellow Field] Domain name should be changed. <script> var numposts = 4; var isList = true; function showrecentposts(json) { for (var i = 0; i < 4; i++) { var entry = json.feed.entry[i]; var postTitle = entry.title.$t; var postUrl; if (i ==...

No comments:

Post a Comment