我发现这个最近的Blogger脚本会显示某个标签或标签的最新帖子。该脚本工作正常。但是,如果我多次使用此脚本,以显示不同标签的最近帖子,并抛出错误:
无法读取属性'标题'未定义的
查看脚本,我无法弄清楚导致这种情况的原因,或者如何修复它。
<script style='text/javascript'>
//<![CDATA[
function labelthumbs(json) {
document.write('<ul class="label_with_thumbs">');
for (var i = 0; i < numposts; i++) {
var entry = json.feed.entry[i];
var posttitle = entry.title.$t;
var posturl;
if (i == json.feed.entry.length) break;
for (var k = 0; k < entry.link.length; k++) {
if (entry.link[k].rel == 'replies' && entry.link[k].type == 'text/html') {
var commenttext = entry.link[k].title;
var commenturl = entry.link[k].href;
}
if (entry.link[k].rel == 'alternate') {
posturl = entry.link[k].href;
break;
}
}
var thumburl;
try {
thumburl = entry.media$thumbnail.url;
} catch (error) {
s = entry.content.$t;
a = s.indexOf("<img");
b = s.indexOf("src=\"", a);
c = s.indexOf("\"", b + 5);
d = s.substr(b + 5, c - b - 5);
if ((a != -1) && (b != -1) && (c != -1) && (d != "")) {
thumburl = d;
} else thumburl = 'http://3.bp.blogspot.com/-O-vjxP03hYo/UXVtH90_iQI/AAAAAAAAAG4/60hz9Rn5FWA/s1600/No+Image+1.gif';
}
var postdate = entry.published.$t;
var cdyear = postdate.substring(0, 4);
var cdmonth = postdate.substring(5, 7);
var cdday = postdate.substring(8, 10);
var monthnames = new Array();
monthnames[1] = "Jan";
monthnames[2] = "Feb";
monthnames[3] = "Mar";
monthnames[4] = "Apr";
monthnames[5] = "May";
monthnames[6] = "June";
monthnames[7] = "July";
monthnames[8] = "Aug";
monthnames[9] = "Sept";
monthnames[10] = "Oct";
monthnames[11] = "Nov";
monthnames[12] = "Dec";
document.write('<li class="clearfix">');
if (showpostthumbnails == true) document.write('<a href="' + posturl + '" target ="_top"><img class="label_thumb" src="' + thumburl + '"/></a>');
document.write('<strong><a href="' + posturl + '" target ="_top">' + posttitle + '</a></strong><br>');
if ("content" in entry) {
var postcontent = entry.content.$t;
} else if ("summary" in entry) {
var postcontent = entry.summary.$t;
} else var postcontent = "";
var re = /<\S[^>]*>/g;
postcontent = postcontent.replace(re, "");
if (showpostsummary == true) {
if (postcontent.length < numchars) {
document.write('');
document.write(postcontent);
document.write('');
} else {
document.write('');
postcontent = postcontent.substring(0, numchars);
var quoteEnd = postcontent.lastIndexOf(" ");
postcontent = postcontent.substring(0, quoteEnd);
document.write(postcontent + '...');
document.write('');
}
}
var towrite = '';
var flag = 0;
document.write('<br>');
if (showpostdate == true) {
towrite = towrite + monthnames[parseInt(cdmonth, 10)] + '-' + cdday + ' - ' + cdyear;
flag = 1;
}
if (showcommentnum == true) {
if (flag == 1) {
towrite = towrite + ' | ';
}
if (commenttext == '1 Comments') commenttext = '1 Comment';
if (commenttext == '0 Comments') commenttext = 'No Comments';
commenttext = '<a href="' + commenturl + '" target ="_top">' + commenttext + '</a>';
towrite = towrite + commenttext;
flag = 1;;
}
if (displaymore == true) {
if (flag == 1) towrite = towrite + ' | ';
towrite = towrite + '<a href="' + posturl + '" class="url" target ="_top">More »</a>';
flag = 1;;
}
document.write(towrite);
document.write('</li>');
if (displayseparator == true) if (i != (numposts - 1)) document.write('');
}
document.write('</ul>');
}
//]]>
</script>
<script type='text/javascript'>
var numposts = 3;
var showpostthumbnails = true;
var displaymore = false;
var displayseparator = true;
var showcommentnum = false;
var showpostdate = false;
var showpostsummary = true;
var numchars = 100;
</script>
<script type="text/javascript" src="/feeds/posts/default/-/video?orderby=published&alt=json-in-script&callback=labelthumbs"></script>
有许多这样的脚本可用,有些类似,有些则没有。但是,似乎他们都抛出了这个错误。是否可能错误是由Feed本身引起的,还是脚本?
具体来说,控制台说错误是这个变量:
var posttitle = entry.title.$t;
答案 0 :(得分:2)
错误是由numposts
变量值超过特定标签中的帖子数
例如,您的标签可能只包含一个帖子,但如果您提供的numposts
的值大于1,那么在for循环的第二次迭代中,它会抛出上述错误,因为他们不会成为任何迭代的帖子
对于工作演示,请检查http://jsbin.com/quniyifuna/1/,此处上述代码的两个实例正常运行,而控制台中没有任何错误
在第二个演示http://jsbin.com/dosuhi/1/中,我给numposts
一个大于该特定标签中的帖子的值,因此在控制台中你会发现与你的相同的错误
答案 1 :(得分:0)
我认为您没有更改标签名称,因为博主只在特定标签下显示相关帖子一次。但是,您必须尝试从JS的最后一行更改博客标签:
<script type="text/javascript" src="/feeds/posts/default/-/video?orderby=published&alt=json-in-script&callback=labelthumbs"></script>
只需将视频标签更改为另一个标签,我确定,它会有效......