Javascript 为什么给出错误:无法读取属性';类别';未定义的?
我有一个旧的过时的脚本,我重新开始工作,并一直在努力改进。它在Blogspot上用于抓取帖子并随机显示,或者按标签显示最近的帖子 以下是脚本:Javascript 为什么给出错误:无法读取属性';类别';未定义的?,javascript,Javascript,我有一个旧的过时的脚本,我重新开始工作,并一直在努力改进。它在Blogspot上用于抓取帖子并随机显示,或者按标签显示最近的帖子 以下是脚本: <script> /*** THIS IS A SCRIPT TO USE WITH BLOGGER/BLOGSPOT HTML GADGET TO SHOW RECENT/RANDOM POSTS. ***/ /*** LET'S ADD THE VARIABLE SETINGS. TO TOGGLE USE TRUE/FA
<script>
/***
THIS IS A SCRIPT TO USE WITH BLOGGER/BLOGSPOT HTML GADGET TO SHOW RECENT/RANDOM POSTS.
***/
/*** LET'S ADD THE VARIABLE SETINGS. TO TOGGLE USE TRUE/FALSE ***/
var domainURL = window.location.hostname;
var className = 'default';
var postCount = 3;
var showPostLabel = 'test';
var showPostContentAmount = 160;
var showPostTitle = 'true';
var showPostTitleAmount = 80;
var showInPostImageSize = 320;
var showPostComments = 'true';
var showPostDate = 'true';
var showPostAuthor = 'true';
var showPostImage = 'true';
var showPostCategory = 'true';
var showPostTags = 'true';
var showPostContent = 'true';
var showCategoryArchive = 'true';
var showRandomPosts = 'true';
/*** LET'S ADD THE RANDOM ARRAY ***/
var RandomArray = [];
var TotalPosts = 0;
var RandomArray = new Array(postCount);
function TotalCount(json) {
TotalPosts = json.feed.openSearch$totalResults.$t
}
document.write('<script type=\"text/javascript\" src=\"https://'+domainURL+'/feeds/posts/default?alt=json-in-script&callback=TotalCount\"><\/script>');
function GenerateNum() {
for (var i = 0; i < postCount ; i++) {
for (var j = 0; j < RandomArray.length; j++){
var RandomNum = Math.floor(Math.random() * (TotalPosts ) + 1);
RandomArray[i] = RandomNum; }
}
}
</script>
<script>
/*** LET'S START THE POST LIST ***/
document.write('<ul class="json-posts '+className+'">');
/*** LET'S START THE FUNCTION ***/
function jsonposts(json)
{
for (var i = 0; i < postCount; i++)
{
/*** LET'S DECLARE VARIABLES ***/
var postVariables = postImage = postUrl = postTitle = postImage = postContent = postArticle = postAuthor = postTags = postCategory = postDate = postUpdated = postComment = postImageURL = postCategoryArchive = postImageURLSize = postAuthorImage = postDateFormat = Y = D = M = m = YY = DD = MM = mm = TT = "";
/*** LET'S GET THE POST LABELS ***/
if (json.feed.entry[i].category != null && json.feed.entry[i].category.length > 0)
{
postCategory += '<a class="json-post-article-category" href="'+domainURL+'/search/label/'+json.feed.entry[i].category[0].term+'">'+json.feed.entry[i].category[0].term+'</a>';
}
/*** LET'S GET THE POST LABELS ***/
if (json.feed.entry[i].category != null)
{
for (var k = 0; k < json.feed.entry[i].category.length; k++)
{
postTags += '<a class="json-post-article-tag-item" href="'+domainURL+'/search/label/'+json.feed.entry[i].category[k].term+'">'+json.feed.entry[i].category[k].term+'</a>';
if(k < json.feed.entry[i].category.length-1)
{
postTags += '<span class="json-post-article-tag-item-seperator">, </span>';
}
}
}
/*** LET'S GET THE POST URL ***/
for (var j = 0; j < json.feed.entry[i].link.length; j++)
{
if (json.feed.entry[i].link[j].rel == 'alternate')
{
break;
}
}
postUrl= '"' + json.feed.entry[i].link[j].href + '"';
/*** LET'S GET THE POST INFO ***/
postCategoryArchive = json.feed.openSearch$totalResults.$t;
/*** LET'S GET THE POST TITLE ***/
if (json.feed.entry[i].title!= null)
{
postTitle= json.feed.entry[i].title.$t.substr(0, showPostTitleAmount);
}
/*** LET'S GET THE POST COMMENT COUNT ***/
if (json.feed.entry[i].thr$total)
{
postComment = '<a href="'+json.feed.entry[i].link[j].href+'#comment-form">'+json.feed.entry[i].thr$total.$t+'</a>';
}
/*** LET'S GET THE POST AUTHOR INFO ***/
postAuthor = json.feed.entry[i].author[0].name.$t.split(' ');
postAuthor = postAuthor.slice(0, 1).join(' ');
postAuthorImage = json.feed.entry[i].author[0].gd$image.src;
/*** LET'S GET THE POST CONTENT ***/
postArticle = json.feed.entry[i].content.$t;
postContent = postArticle.replace(/(<([^>]+)>)/ig,'').substring(0, showPostContentAmount);
/*** LET'S FORMAT THE POST DATE MONTH ***/
postDateFormat= ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
/*** LET'S GET THE POST DATE ***/
postDate= json.feed.entry[i].published.$t.substring(0,10);
/*** LET'S DO THE POST DATE FORMAT ***/
Y = postDate.substring(0, 4);
m = postDate.substring(5, 7);
D = postDate.substring(8, 10);
M = postDateFormat[parseInt(m - 1)];
/*** LET'S GET THE UPDATED POST DATE ***/
postUpdated= json.feed.entry[i].updated.$t.substring(0, 16);
/*** LET'S DO THE UPDATED POST DATE FORMAT ***/
YY = postUpdated.substring(0, 4);
mm = postUpdated.substring(5, 7);
DD = postUpdated.substring(8, 10);
TT = postUpdated.substring(11, 16);
MM = postDateFormat[parseInt(mm - 1)];
/*** LET'S GET THE POST IMAGE ***/
if (json.feed.entry[i].media$thumbnail)
{
postImageURL = json.feed.entry[i].media$thumbnail.url;
postImageURLSize = postImageURL.replace("/s72-c/","/s"+showInPostImageSize+"/");
postImageURLSize = postImageURL.replace("default.jpg","mqdefault.jpg");
}
/*** LET'S OPEN THE POST ARTICLE ***/
document.write('<li class="json-post-article"><article itemprop="blogPost" itemscope="itemscope" itemtype="https://schema.org/BlogPosting">');
/*** LET'S SHOW THE POST CATEGORY ***/
if (showPostCategory == 'true')
{
document.write('<div class="json-post-article-category"><span class="json-post-article-category-label">Category: </span>'+postCategory+'</div>');
}
/*** LET'S SHOW THE POST IMAGE ***/
if (showPostImage == 'true')
{
document.write('<a class="json-post-article-featured-image-link" href="'+postUrl+'" itemprop="url"><figure class="json-post-article-featured-image" style="background-image: url('+postImage+')"></figure></a>');
}
/*** LET'S OPEN THE POST INFO BOX ***/
document.write('<div>');
/*** LET'S SHOW THE POST TITLE ***/
if (showPostTitle == 'true')
{
document.write('<h4 class="json-post-article-title" itemprop="name"><a class="json-post-article-title-link" href="'+postUrl+'" itemprop="url">'+postTitle+'</a></h4>');
}
/*** LET'S SHOW THE POST AUTHOR ***/
if (showPostAuthor == 'true')
{
document.write('<div class="json-post-article-author"><span class="json-post-article-author-label" itemprop="author">Author: </span><figure class="json-post-article-author-avatar" style="background: url(https:'+postAuthorImage+')"></figure><span class="json-post-article-author-name">'+postAuthor+'</span></div>');
}
/*** LET'S SHOW THE POST DATE ***/
if (showPostDate == 'true')
{
document.write('<div class="json-post-article-date" itemprop="datePublished"><span class="json-post-article-date-label">Date: </span><span class="json-post-article-date-month">'+M+'</span> <span class="json-post-article-date-day">'+D+'</span> <span class="json-post-article-date-year">'+Y+'</span></div>');
}
/*** LET'S SHOW THE POST CONTENT ***/
if (showPostContent == 'true')
{
document.write('<p class="json-post-article-content">'+postContent+'...</p>');
}
/*** LET'S SHOW THE POST TAGS ***/
if (showPostTags == 'true')
{
document.write('<div class="json-post-article-tags"><span class="json-post-article-tags-label">Tags: </span>'+postTags+'</div>');
}
/*** LET'S SHOW THE POST COMMENT COUNT ***/
if (showPostComments == 'true')
{
document.write('<div class="json-post-article-comment-count"><span class="json-post-article-comment-count-label">Comments: </span><span class="json-post-article-comment-count-number">'+postComment+'</span></div>');
}
/*** LET'S CLOSE THE POST INFO BOX ***/
document.write('</div>');
/*** LET'S CLOSE THE POST ARTICLE ***/
document.write('</article></li>');
}
/*** LET'S SHOW THE POST CATEGORY ARCHIVE LINK ***/
if (showCategoryArchive == 'true')
{
document.write('<a class="json-post-article-category-more" href="'+domainURL+'/search/label/'+showPostLabel+'">More</a>');
}
}
/*** LET'S SHOW THE POST AS RANDOM OR ELSE AS RECENT ***/
if (showRandomPosts == 'true')
{
for (var i = 0; i < postCount ; i++)
{
GenerateNum();
document.write('<script type=\"text/javascript\" src=\"https://'+domainURL+'/feeds/posts/default?alt=json-in-script&start-index=' + RandomArray[i] + '&max-results=1&callback=jsonposts\"><\/script>')
}
}
else
{
document.write('<script type=\"text/javascript\" src=\"https://'+domainURL+'/feeds/posts/default/-/'+showPostLabel+'?alt=json-in-script&callback=jsonposts\"><\/script>')
}
document.write('</ul>');
</script>
很难理解JSON的结构,因为我们不知道传递给
jsonposts
函数的是什么。该错误很好地描述了问题所在,但您需要使用或获取更多信息
我看到您在代码中9次访问类别
属性,每次都是在json.feed.entry[I]
上访问它们,这让我相信entry
数组中的entry
元素之一是未定义的
。一种可能是postCount
高于json.feed.entry
数组的长度
看看我的复制品,看看什么时候会发生
var postCount=5;
var json={
提要:{
条目:[{category:{term:'test'},{category:{term:'test2'}}]
}
}
对于(变量i=0;i }
postCount
高于json.feed.entry
数组不是问题。我仍在努力理解您的其他建议。如果您将showRandomPosts
设置为非true,则不会出现错误。问题必须是随机数组代码。