/    Sign up×
Community /Pin to ProfileBookmark

Fixing Avoid document.write() error

Hi
can someone help me to fix javascript code for my blog, its loads by default from the template
This is part of the code

“`
while(rangkumanPosts<relmaxposts)
{
if(relurls[r]!=dirURL)
{
relhasil=”<li class=’news-title clearfix’>”;
relhasil+=”<a href='”+relurls[r]+”‘ ><img src='”+relgambar[r]+”‘ alt='”+reljudul[r]+”‘/></a>”;
relhasil+=”<a href='”+relurls[r]+”‘ alt='”+reljudul[r]+”‘>”+reljudul[r]+”</a>”;
relhasil+=”<span class=’news-text’>”+relcuplikan[r]+” … <a href='”+relurls[r]+”‘ aria-label='”+reljudul[r]+”‘ target=’_top’>”+morelink+”</a><span class=’news-text’>”;
relhasil+=”</li>”;
document.write(relhasil);
rangkumanPosts++;
if(rangkumanPosts==relmaxposts)
{
break
}
}
if(r<reljudul.length-1)
“`

>

document.write(relhasil);

i failed in lighthouse test in document write part, lighthouse suggest to avoid using that command, tried to delete the line but some feature in my blog become not working

Thank you

to post a comment
Full-stack DeveloperJavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@SempervivumSep 28.2020 — lighthouse suggest to avoid using that command[/quote]Lighthouse is right, it's better to avoid document.write. In order to advice how to do so it's necessary to know the HTML context where these li's should be inserted, in detail the ul where they are located in. Best would be if you could post the URL of your blog.
Copy linkTweet thisAlerts:
@informatikaauthorSep 28.2020 — @Sempervivum#1623740

Hi semper

seems i cannot edit my previous post (tried to post the full code)

thats the problem , the xml template was build using artiseer (offiline aplications) and i havent finished yet..let me see if i can just use some temporary blog

but in the meantime heres the full code
``<i>
</i>&lt;script type='text/javascript'&gt;/*&lt;![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("&lt;");
for(var i=0;
i&lt;cuplik.length;
i++)
{
if(cuplik[i].indexOf("&gt;")!=-1)
{
cuplik[i]=cuplik[i].substring(cuplik[i].indexOf("&gt;")+1,cuplik[i].length)
}
}
cuplik=cuplik.join("");
cuplik=cuplik.substring(0,panjang-1);
return cuplik
}
function relpostimgthum(json)
{
for(var i=0;
i&lt;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="https://lh3.ggpht.com/_xcD4JK_dIjU/SnamIh0KTCI/AAAAAAAADMA/hLjqmEbdtkw/d/noimagethumb.gif"
}
relgambar[relnum]=postimg;
for(var k=0;
k&lt;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&lt;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&lt;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&lt;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&lt;relmaxposts)
{
if(relurls[r]!=dirURL)
{
relhasil="&lt;li class='news-title clearfix'&gt;";
relhasil+="&lt;a href='"+relurls[r]+"' &gt;&lt;img src='"+relgambar[r]+"' alt='"+reljudul[r]+"'/&gt;&lt;/a&gt;";
relhasil+="&lt;a href='"+relurls[r]+"' alt='"+reljudul[r]+"'&gt;"+reljudul[r]+"&lt;/a&gt;";
relhasil+="&lt;span class='news-text'&gt;"+relcuplikan[r]+" ... &lt;a href='"+relurls[r]+"' aria-label='"+reljudul[r]+"' target='_top'&gt;"+morelink+"&lt;/a&gt;&lt;span class='news-text'&gt;";
relhasil+="&lt;/li&gt;";
document.write(relhasil);
rangkumanPosts++;
if(rangkumanPosts==relmaxposts)
{
break
}
}
if(r&lt;reljudul.length-1)
{
r++
}
else
{
r=0
}
if(r==rini)
{
break
}
}
}
/*]]&gt;*/&lt;/script&gt;<i>
</i>
``
Copy linkTweet thisAlerts:
@SempervivumSep 28.2020 — It's necessary to view the **HTML** around that script, not the javascript itself. There should be an <ul> tag where the <li>'s are inserted. This might be created by javascript either but I cannot find it in the code you posted.
×

Success!

Help @informatika spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.23,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...