简体   繁体   中英

RandomNum is undefined, and I don't understand why

I took this script tutorial from here , and I tried to combine the random function from this similar tutorial here , except make it a setting, and it says RandomNum is undefined. Why, and how do I fix it?

Javascript:

/*** LET'S ADD THE VARIABLE SETTINGS. 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 GENERATE RANDOM POSTS ***/
if (showRandomPosts == 'true')
{
  var RandomArray = [];
  var TotalPosts  = 0;
  var RandomArray = new Array(postCount);
  
  function TotalCount(json)
  {
    TotalPosts   = json.feed.openSearch$totalResults.$t;
  }
  document.write('<script 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; 
      }
    }
  }
}

/*** LET'S START THE POST LIST ***/
function jsonposts(json)
{
  document.write('<ul class="json-posts '+className+'">');
  for (var i = 0; i < postCount; i++)
  {
    /*** LET'S DECLARE VARIABLES ***/
    var postVariables= postImage = postUrl = postTitle = postImage = postContent = postArticle = postAuthor = postLabel = 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)
    {
      for (var k = 0; k < json.feed.entry[i].category.length; k++) 
      {
        postLabel += '<a class="json-post-article-tags-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)
        { 
          postLabel += '<span class="json-post-article-tags-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+"/");
      postImage= "'" + postImageURLSize.replace("?imgmax=800","") + "'";
    }

    /*** LET'S ADD VARIOUS IMAGE TYPE SUPPORT***/
    else if (json.feed.entry[i].content.$t.match(/src=(.+?[\.jpg|\.gif|\.png]")/) != null)
    {
      postImage =  json.feed.entry[i].content.$t.match(/src=(.+?[\.jpg|\.gif|\.png]")/)[1];
    }

    /*** LET'S OPEN THE POST ARTICLE ***/
    document.write('<li class="json-post-article-'+[i]+'">');

    /*** LET'S SHOW THE POST CATEGORY ***/
    if (showPostCategory == 'true')
    {
      document.write('<div class="json-post-article-tags"><span class="json-post-article-tags-label"></span>'+postLabel+'</div>');
    }

    /*** LET'S SHOW THE POST IMAGE ***/
    if (showPostImage == 'true')
    {
      document.write('<a class="json-post-article-featured-image-link" href="'+postUrl+'"><figure class="json-post-article-featured-image" style="background: url('+postImage+') no-repeat center center / cover !important"></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"><a class="json-post-article-title-link" href="'+postUrl+'">'+postTitle+'</a></h4>');
    }

    /*** LET'S SHOW THE POST AUTHOR ***/
    if (showPostAuthor == 'true')
    {
      document.write('<span class="json-post-article-author-label"></span><figure class="json-post-article-author-avatar" style="background: url(https:'+postAuthorImage+') no-repeat center center /cover !important"></figure><span class="json-post-article-author-avatar">'+postAuthor+'</span>');
    }

    /*** LET'S SHOW THE POST DATE ***/
    if (showPostDate == 'true')
    {
      document.write('<div class="json-post-article-date"><span class="json-post-article-date-label"></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"></span>'+postLabel+'</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('</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 STOP THE POST LIST ***/
  document.write('</ul>');

}

/*** 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 src="https://'+domainURL+'/feeds/posts/default/-/'+showPostLabel+'?alt=json-in-script&start-index='+RandomNum+'&max-results=1&callback=jsonposts"></'+'script>')
  }
}
else 
{
  document.write('<script src="https://'+domainURL+'/feeds/posts/default/-/'+showPostLabel+'?alt=json-in-script&callback=jsonposts"></'+'script>');
}

Is is the same code with domainURL and showPostLabel settings changed so it can be properly tested.

 /*** LET'S ADD THE VARIABLE SETINGS. TO TOGGLE USE TRUE/FALSE ***/ var domainURL = "www.mybloggertricks.com"; var className = "default"; var postCount = 3; var showPostLabel = "Widgets"; 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 GENERATE RANDOM POSTS ***/ if (showRandomPosts == 'true') { var RandomArray = []; var TotalPosts = 0; var RandomArray = new Array(postCount); function TotalCount(json) { TotalPosts = json.feed.openSearch$totalResults.$t; } document.write('<script 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; } } } } /*** LET'S START THE POST LIST ***/ function jsonposts(json) { document.write('<ul class="json-posts '+className+'">'); for (var i = 0; i < postCount; i++) { /*** LET'S DECLARE VARIABLES ***/ var postVariables= postImage = postUrl = postTitle = postImage = postContent = postArticle = postAuthor = postLabel = 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) { for (var k = 0; k < json.feed.entry[i].category.length; k++) { postLabel += '<a class="json-post-article-tags-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) { postLabel += '<span class="json-post-article-tags-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+"/"); postImage= "'" + postImageURLSize.replace("?imgmax=800","") + "'"; } /*** LET'S ADD VARIOUS IMAGE TYPE SUPPORT***/ else if (json.feed.entry[i].content.$t.match(/src=(.+?[\\.jpg|\\.gif|\\.png]")/) != null) { postImage = json.feed.entry[i].content.$t.match(/src=(.+?[\\.jpg|\\.gif|\\.png]")/)[1]; } /*** LET'S OPEN THE POST ARTICLE ***/ document.write('<li class="json-post-article-'+[i]+'">'); /*** LET'S SHOW THE POST CATEGORY ***/ if (showPostCategory == 'true') { document.write('<div class="json-post-article-tags"><span class="json-post-article-tags-label"></span>'+postLabel+'</div>'); } /*** LET'S SHOW THE POST IMAGE ***/ if (showPostImage == 'true') { document.write('<a class="json-post-article-featured-image-link" href="'+postUrl+'"><figure class="json-post-article-featured-image" style="background: url('+postImage+') no-repeat center center / cover !important"></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"><a class="json-post-article-title-link" href="'+postUrl+'">'+postTitle+'</a></h4>'); } /*** LET'S SHOW THE POST AUTHOR ***/ if (showPostAuthor == 'true') { document.write('<span class="json-post-article-author-label"></span><figure class="json-post-article-author-avatar" style="background: url(https:'+postAuthorImage+') no-repeat center center /cover !important"></figure><span class="json-post-article-author-avatar">'+postAuthor+'</span>'); } /*** LET'S SHOW THE POST DATE ***/ if (showPostDate == 'true') { document.write('<div class="json-post-article-date"><span class="json-post-article-date-label"></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"></span>'+postLabel+'</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('</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 STOP THE POST LIST ***/ document.write('</ul>'); } /*** 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 src="https://'+domainURL+'/feeds/posts/default/-/'+showPostLabel+'?alt=json-in-script&start-index='+RandomNum+'&max-results=1&callback=jsonposts"></'+'script>') } } else { document.write('<script src="https://'+domainURL+'/feeds/posts/default/-/'+showPostLabel+'?alt=json-in-script&callback=jsonposts"></'+'script>'); }

FYI: if showRandomPosts is set to false , then the script works fine.

FYI: if showRandomPosts is set to true, then the script works fine.

Your function RandomNum() is inside of that if statement which causes your function to only execute when showRandomPosts = true

But if I am understanding correctly you only want to show random posts if your variable is true. If you want to continiously use the RandomNum() function you could just remove your if statement under /*** LET'S GENERATE RANDOM POSTS ***/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM