Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help me fix this document.write javascript code

Status
Not open for further replies.

rosarij

Technical User
Oct 9, 2020
1
0
0
ID

Hello Guys
Can someone help me with this javascript code? accessibility test and lighhouse test gave me warning about the use of document.write

For users on slow connections, external scripts dynamically injected via `document.write()` can delay page load by tens of seconds

image resizing triggered several times on my homepage and post page with document.write method
can someone suggest me better solution for document.write alternative?or fix this code

thanks

Code:
<article><div class='post hentry'>
          <a expr:name='data:post.id'/>
          <b:if cond='data:blog.pageType != &quot;item&quot;'>
      <b:if cond='data:blog.pageType != &quot;static_page&quot;'>
      <b:if cond='data:blog.pageType == &quot;index&quot;'>
      <b:if cond='data:post.isFirstPost'> 
      <script type='text/javascript'>
      //<![CDATA[
      function bp_thumbnail_resize(image_url,post_title)
      {
      var image_size=150;
      var show_default_thumbnail=true;
      var default_thumbnail="img.jpg";
      if(show_default_thumbnail == true && image_url == "") image_url= default_thumbnail;
      image_tag='<img src="'+image_url.replace('/s72-c/','/s'+image_size+'-c/')+'" class="post-thumbnail" alt="more '+post_title+'"/>';
      if(image_url!="") return image_tag; else return "";
      }
      //]]>
      </script>
      </b:if>
      <script type='text/javascript'>
      [b]document.write[/b](bp_thumbnail_resize(&quot;<data:post.thumbnailUrl/>&quot;,&quot;<data:post.title/>&quot;));
      </script>
      </b:if>
      </b:if>

And
Code:
<!--Related post-->
<b:if cond='data:blog.pageType == &quot;item&quot;'>
<style>
#related_posts h4{color: #fff;font-size: 12px;background: #1A7C62;padding: 3px 10px;margin: 0 0 5px;}
#relpost_img_sum{margin:0;padding:0;line-height:16px;font-size: 11px;}
#relpost_img_sum:hover{background:0}
#relpost_img_sum ul{list-style-type:none;margin:0;padding:0}
#relpost_img_sum li{min-height: 74px;border-bottom: 1px solid #F4F3F3;list-style: none;margin: 0 0 5px;padding: 5px;}
#relpost_img_sum .news-title{display:block;font-weight:bold!important;margin-bottom:5px;font-size: 14px;}
#relpost_img_sum .news-text{display:block;text-align:justify;font-weight:normal;text-transform:none;color:#333;font-size: 13px;}
#relpost_img_sum img{float:left;margin-right:7px;border:solid 1px #ddd;width:72px;height:72px;background:#fff}
</style>
<script type='text/javascript'>/*<![CDATA[*/var relnum=0;var relmaxposts=5;var numchars=135;var morelink=" ";eval(function(p,a,c,k,e,r){e=function(c){return(c<a?'':e(parseInt(c/a)))+((c=c%a)>35?String.fromCharCode(c+29):c.toString(36))};if(!''.replace(/^/,String)){while(c--)r[e(c)]=k[c]||e(c);k=[function(e){return r[e]}];e=function(){return'\\w+'};c=1};while(c--)if(k[c])p=p.replace(new RegExp('\\b'+e(c)+'\\b','g'),k[c]);return p}('2 4=f g();2 5=f g();2 9=f g();2 b=f g();s K(L,M){2 7=L.19("<");l(2 i=0;i<7.3;i++){8(7[i].N(">")!=-1){7[i]=7[i].O(7[i].N(">")+1,7[i].3)}}7=7.1a("");7=7.O(0,M-1);y 7}s 1b(z){l(2 i=0;i<z.P.6.3;i++){2 6=z.P.6[i];4[n]=6.A.$t;u="";8("Q"B 6){u=6.Q.$t}C 8("R"B 6){u=6.R.$t}9[n]=K(u,1c);8("S$T"B 6){D=6.S$T.1d}C{D="1e://1f.1g.1h/1i/1j/1k/1l/d/1m.1n"}b[n]=D;l(2 k=0;k<6.E.3;k++){8(6.E[k].U==\'1o\'){5[n]=6.E[k].v;F}}n++}}s V(a,e){l(2 j=0;j<a.3;j++)8(a[j]==e)y 1p;y 1q}s 1r(){2 m=f g(0);2 o=f g(0);2 p=f g(0);2 q=f g(0);l(2 i=0;i<5.3;i++){8(!V(m,5[i])){m.3+=1;m[m.3-1]=5[i];o.3+=1;o[o.3-1]=4[i];p.3+=1;p[p.3-1]=9[i];q.3+=1;q[q.3-1]=b[i]}}4=o;5=m;9=p;b=q;l(2 i=0;i<4.3;i++){2 c=w.W((4.3-1)*w.X());2 Y=4[i];2 Z=5[i];2 10=9[i];2 11=b[i];4[i]=4[c];5[i]=5[c];9[i]=9[c];b[i]=b[c];4[c]=Y;5[c]=Z;9[c]=10;b[c]=11}2 x=0;2 r=w.W((4.3-1)*w.X());2 12=r;2 h;2 13=14.1s;1t(x<15){8(5[r]!=13){h="<16 G=\'H-A 1u\'>";h+="<a v=\'"+5[r]+"\' U=\'1v\' I=\'J\' A=\'"+4[r]+"\'><1w 1x=\'"+b[r]+"\' /></a>";h+="<a v=\'"+5[r]+"\' I=\'J\'>"+4[r]+"</a>";h+="<17 G=\'H-18\'>"+9[r]+" ... <a v=\'"+5[r]+"\' I=\'J\'>"+1y+"</a><17 G=\'H-18\'>";h+="</16>";14.1z(h);x++;8(x==15){F}}8(r<4.3-1){r++}C{r=0}8(r==12){F}}}',62,98,'||var|length|reljudul|relurls|entry|cuplik|if|relcuplikan||relgambar|informasi|||new|Array|relhasil||||for|tmp|relnum|tmp2|tmp3|tmp4||function||postcontent|href|Math|rangkumanPosts|return|json|title|in|else|postimg|link|break|class|news|target|_top|saringtags|suchas|panjang|indexOf|substring|feed|content|summary|media|thumbnail|rel|contains|floor|random|tempJudul|tempUrls|tempCuplikan|tempGambar|rini|dirURL|document|relmaxposts|li|span|text|split|join|relpostimgthum|numchars|url|http|lh3|ggpht|com|_xcD4JK_dIjU|SnamIh0KTCI|AAAAAAAADMA|hLjqmEbdtkw|noimagethumb|gif|alternate|true|false|relatpost|URL|while|clearfix|nofollow|img|src|morelink|write'.split('|'),0,{}))/*]]>*/</script>
</b:if>

but it seems the script is encrypted , i used unpacker tool and this is the result


Code:
<script type='text/javascript'>/*<![CDATA[*/var relnum=0;
var relmaxposts=5;
var numchars=135;
var morelink=" ";
var reljudul=new Array();
var relurls=new Array();
var relcuplikan=new Array();
var relgambar=new Array();
function saringtags(suchas,panjang)
	{
	var cuplik=suchas.split("<");
	for(var i=0;
	i<cuplik.length;
	i++)
		{
		if(cuplik[i].indexOf(">")!=-1)
			{
			cuplik[i]=cuplik[i].substring(cuplik[i].indexOf(">")+1,cuplik[i].length)
		}
	}
	cuplik=cuplik.join("");
	cuplik=cuplik.substring(0,panjang-1);
	return cuplik
}
function relpostimgthum(json)
	{
	for(var i=0;
	i<json.feed.entry.length;
	i++)
		{
		var entry=json.feed.entry[i];
		reljudul[relnum]=entry.title.$t;
		postcontent="";
		if("content"in entry)
			{
			postcontent=entry.content.$t
		}
		else if("summary"in entry)
			{
			postcontent=entry.summary.$t
		}
		relcuplikan[relnum]=saringtags(postcontent,numchars);
		if("media$thumbnail"in entry)
			{
			postimg=entry.media$thumbnail.url
		}
		else
			{
			postimg="noimagethumb.gif"
		}
		relgambar[relnum]=postimg;
		for(var k=0;
		k<entry.link.length;
		k++)
			{
			if(entry.link[k].rel=='alternate')
				{
				relurls[relnum]=entry.link[k].href;
				break
			}
		}
		relnum++
	}
}
function contains(a,e)
	{
	for(var j=0;
	j<a.length;
	j++)if(a[j]==e)return true;
	return false
}
function relatpost()
	{
	var tmp=new Array(0);
	var tmp2=new Array(0);
	var tmp3=new Array(0);
	var tmp4=new Array(0);
	for(var i=0;
	i<relurls.length;
	i++)
		{
		if(!contains(tmp,relurls[i]))
			{
			tmp.length+=1;
			tmp[tmp.length-1]=relurls[i];
			tmp2.length+=1;
			tmp2[tmp2.length-1]=reljudul[i];
			tmp3.length+=1;
			tmp3[tmp3.length-1]=relcuplikan[i];
			tmp4.length+=1;
			tmp4[tmp4.length-1]=relgambar[i]
		}
	}
	reljudul=tmp2;
	relurls=tmp;
	relcuplikan=tmp3;
	relgambar=tmp4;
	for(var i=0;
	i<reljudul.length;
	i++)
		{
		var informasi=Math.floor((reljudul.length-1)*Math.random());
		var tempJudul=reljudul[i];
		var tempUrls=relurls[i];
		var tempCuplikan=relcuplikan[i];
		var tempGambar=relgambar[i];
		reljudul[i]=reljudul[informasi];
		relurls[i]=relurls[informasi];
		relcuplikan[i]=relcuplikan[informasi];
		relgambar[i]=relgambar[informasi];
		reljudul[informasi]=tempJudul;
		relurls[informasi]=tempUrls;
		relcuplikan[informasi]=tempCuplikan;
		relgambar[informasi]=tempGambar
	}
	var rangkumanPosts=0;
	var r=Math.floor((reljudul.length-1)*Math.random());
	var rini=r;
	var relhasil;
	var dirURL=document.URL;
	while(rangkumanPosts<relmaxposts)
		{
		if(relurls[r]!=dirURL)
			{
			relhasil="<li class='news-title clearfix'>";
			relhasil+="<img src='"+relgambar[r]+"' alt='artikel tentang "+reljudul[r]+"'/></a>";
			relhasil+="<a href='"+relurls[r]+"' alt='"+reljudul[r]+"'>"+reljudul[r]+"</a>";
			relhasil+="<span class='news-text'>"+relcuplikan[r]+" ... ";
			relhasil+="</li>";
			[b]document.write[/b](relhasil);
			rangkumanPosts++;
			if(rangkumanPosts==relmaxposts)
				{
				break
			}
		}
		if(r<reljudul.length-1)
			{
			r++
		}
		else
			{
			r=0
		}
		if(r==rini)
			{
			break
		}
	}
}
/*]]>*/</script>

And the result is displayed here

Code:
      <div id='related_posts' style='margin-top:5px'>
        <h3>Related Articles :</h3>

        <b:loop values='data:post.labels' var='label'>
          <script expr:src='&quot;/feeds/posts/default/-/&quot; + data:label.name + &quot;?alt=json-in-script&amp;callback=relpostimgthum&amp;max-results=50&quot;' type='text/javascript'/>
        </b:loop>
        <ul id='relpost_img_sum'>
          <script type='text/javascript'>relatpost();</script>
        </ul>
      </div>
      </div>
      </b:if>

 
For a standard print type statement, you are best off creating a HTML element with an ID and then using that as the reference to update it.

suggest you look at the DOM, there are many docs on line about how to dynamically add HTML elements to it, and packages like JQuery that will make it easier and handle browser support etc for your list part of the question.

dynamically changing content is never overly great for disabled users tools such as screen readers, but some are better than others.

Greg Griffiths
Livelink Certified Developer & ECM Global Star Champion 2005 & 2006
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top