← Back to blog

Json Widget အတွက် Javascript ထည့်သွင်းခြင်း

How to

(၁) Setting များ ပြင်ဆင်ခြင်းSettings >> Site Feed မှ Allow Blog Feed အား Short အဖြစ် ပြောင်းလဲ သက်မှတ်ပေးရမည်။

Settings >> Archiving မှ Enable Post Pages? အား Yes အဖြစ် ပြောင်းလဲ သက်မှတ်ပေးရမည်။

အဆင့် (၂) Javascript ထည့်သွင်းခြင်းအောက်ပါ Code ကို </head> ၏ အပေါ်နားတွင် ထည့်ပေးပါ။<script type=’text/javascript’>// Developed by Hoctro – All rights reserved 2007// This credit must be included in all your derived usages.// “cb” is intended to be a common library, where different widgets would// utitlize the shared operations such as getTitle, getLink, etc. from a json object.var cb = {// search function requires these parameters:// 1. query: a blogger address, such as “hoctro.blogspot.com”,// 2. type: type of return data, either “comments” or “posts”,// 3. start: the start-index parameter (where to start extracting data)// 4. increment: the number of elements the json will get back. (the smaller value, the faster time to travel back)// 5. func: the returned function the json object will feed.search: function(query, type, start, increment, func) {var script = document.createElement(‘script’);script.setAttribute(‘src’, ‘http://’ + query + ‘/feeds/’ + type + ‘/default?alt=json-in-script&start-index=’+ start + ‘&max-results=’ + increment + ‘&callback=’ + func + ‘&orderby=published’);script.setAttribute(‘type’, ‘text/javascript’);document.documentElement.firstChild.appendChild(script);},// searchLabel function return a result of posts w/ a label query// it requires these parameters:// 1. query: a blogger address, such as “hoctro.blogspot.com”,// 2. an array of labels// 3. func: the returned function the json object will feed.searchLabel: function(query, label, func) {var script = document.createElement(‘script’);script.setAttribute(‘src’, ‘http://’ + query + ‘/feeds/posts/default/-/’ + encodeURIComponent(label) +’?alt=json-in-script&callback=’ + func + ‘&orderby=published’);script.setAttribute(‘type’, ‘text/javascript’);document.documentElement.firstChild.appendChild(script);},// getTotalResults needs the json object, and it’ll return the total number of comments (or posts) of the blog.getTotalResults: function(json) {return json.feed.openSearch$totalResults.$t;},// getStartIndex gets the start index of a search inside an json object.getStartIndex: function(json) {return json.feed.openSearch$startIndex.$t;},// getLink return a href link if “name” matches the content inside “a” tags) of the linkgetLink: function(entry, name) {var alturl;for (var k = 0; k < entry.link.length; k++) {if (entry.link[k].rel == name)alturl = entry.link[k].href;}return alturl;},// getTitle gets the title of the title of an entry of a json object.getTitle: function(entry) {return entry.title.$t;},// getContent gets the content inside an entry of a json object.getContent: function(entry) {return entry.content.$t;},// getCommentAuthor: gets the commenter name inside an entry of a json object.getCommentAuthor: function(entry) {return entry.author[0].name.$t;},// Given a json label search, this function return the decoded label.getLabelFromURL: function(json) {for (var l = 0; l < json.feed.link.length; l++) {if (json.feed.link[l].rel == ‘alternate’) {var raw = json.feed.link[l].href;// The next two lines are borrowed from Ramani’s Neo Template// code. Thanks Ramani!var label = raw.substr(raw.lastIndexOf(‘/’)+1);return decodeURIComponent(label);}}},txt : function (s) {return s + ” Widget by <a href=’http://hoctro.blogspot.com” + “‘>Hoctro</a>”;}};</script>

အထက်ပါ Code သည် ဘုံသုံးနိုင်သော လုပ်ဆောင်ချက်များ ပါဝင်ပါသည်။ Json Object ၏ ခေါင်းစဉ်ရှာခြင်း၊ လင့်ရှာခြင်း စသော လုပ်ဆောင်ချက်များအား ရေးသားထားခြင်းဖြစ်သည်။ ထို့ကြောင့် ထို Code ကို ထည့်သွင်းပြီးနောက်ပိုင်း ဤ Json Object ကို အသုံးပြုထားသော မည့်သည် လုပ်ဆောင်ချက်အတွက်မဆို တစ်ခါ ထည့်သွင်းထားရုံဖြင့် ထပ်မံ ထည့်သွင်းစရာ မလိုတော့ပါ။

Leave a comment

Your email address will not be published. Required fields are marked *