javascript - Undefined Variable In document.write -
i keep getting "toggleonlyrelatedposts not defined " in chrome console , script doesn't work. i'm working on script , i've gotten lost after adding many variables. i'm not @ sort of thing , me looks defined, guess it's not. chrome marks error @ document.write
here script:
<script type="text/javascript"> //<![cdata[ function postgrabber(json) { // magic (var = 0; < json.feed.entry.length; i++) { (var j = 0; j < json.feed.entry[i].link.length; j++) { if (json.feed.entry[i].link[j].rel == 'alternate') { var posturl = json.feed.entry[i].link[j].href; break; } } // thumbnail stuff var orgimgurl = json.feed.entry[i].media$thumbnail.url ? json.feed.entry[i].media$thumbnail.url : 'http://1.bp.blogspot.com/-mxinhrjwpbo/vd6fqbvi74i/aaaaaaaacn8/lsluldeorog/s72-c/noimage-chalkboard.jpg'; var newimgurl = orgimgurl.replace('s72-c', 's' + imgsize + '-c'); var imgtag = '<a class="item-link-post" href="' + posturl + '"><img class="item-img-thumbnail" src="' + newimgurl + '" width="' + imgsize + '" height="' + imgsize + '"/></a>'; var authorname = json.feed.entry[i].author[0].name.$t; var authorurl = json.feed.entry[i].author[0].uri.$t; var authororiimgurl = json.feed.entry[i].author[0].gd$image.src; var authornewimgurl = authororiimgurl.replace('s512-c', 's' + authorimgsize + '-c'); var authorimgtag = '<a class="item-link-author" href="' + authorurl + '" target="_blank" rel="nofollow"><img class="item-img-author" src="' + authornewimgurl + '" alt="' + authorname + '"/></a>'; var postlabel = json.feed.category[i].term; var postlabelurl = '/-/' + postlabel + ''; // standard stuff var posttitle = json.feed.entry[i].title.$t; var postcommentcount = json.feed.entry[i].thr$total.$t; var postsummary = json.feed.entry[i].summary.$t; var entryshort = postsummary.substring(0, '' + summarylength + ''); var entryend = entryshort.lastindexof(" "); var postcontent = entryshort.substring(0, entryend) + '...'; var postdate = json.feed.entry[i].updated.$t ? json.feed.entry[i].updated.$t : json.feed.entry[i].published.$t; var shortdate = postdate.substring(0,10); // let's make options here var toggleimg = showimg ? '' + imgtag + '' : ''; var toggletitle = showtitle ? '<h1 class="item-title">' + posttitle + '</h1>' : ''; var togglesummary = showsummary ? '<p class="item-snippet">' + postcontent + '</p>' : ''; var toggledate = showdate ? '<span class="item-date">' + shortdate + '</span>' : ''; var toggleauthorimg = showauthorimg ? '' + authorimgtag + '' : ''; var togglecommentcount = showcommentcount ? '<span class="item-comment-count">' + postcommentcount + '</span>' : ''; var toggleonlyrelatedposts = showonlyrelatedposts ? '' + postlabelurl + '' : ''; // output var itempost = '<div class="item-post"><div class="item-imgs">' + toggleimg + toggleauthorimg + '</div>' + togglecommentcount + '<a class="item-link" href=' + posturl + '>' + toggletitle + '</a>' + togglesummary + toggledate + '</div>'; // let's write down document.write(itempost); } } //]]> </script> <script type="text/javascript"> //<![cdata[ var imgsize = 96; var summarylength = 142; var authorimgsize = 36; var showimg = true; var showtitle = true; var showsummary = true; var showdate = true; var showauthorimg = true; var showcommentcount = true; var showonlyrelatedposts = true; document.write('<script type="text/javascript" src="/feeds/posts/summary' + toggleonlyrelatedposts + '?orderby=published&max-results=5&alt=json-in-script&callback=postgrabber"><\/script>'); //]]> </script>
the variable toggleonlyrelatedposts
declared in scope of function postgrabber
. therefore indeed undefined in line document.write
, try use it.
if wish use variables outside function have declare them outside function.
read on concept of 'scope', it's pretty fundamental knowledge in programming language.
Comments
Post a Comment