I originally set out to do an XML parse (single node) using jQuery, but had no luck so I ended up using plain Javascript instead. The code is found in this thread: Parse XML using JS
Now I can see that the way I use XMLhttpRequest function is deprecated and might be removed in the future, which would ruin quite a lot on my homepage XMLhttpRequest and Google inspector also reports
Synchronous XMLHttpRequest outside of workers is in the process of being removed from the web platform as it has detrimental effects to the end user's experience
I would like to re-open the original question, and get help rewriting the below to load my XML without using the async=false in my code
xhttp.open("GET",filename,false);
The code is being invoked about 9/10 down in my HMTL-code, but simply changing "false" to "true" is no good, as nothing loads at all (I guess the code moves on before it has a chance to insert the XML-data?). With my weak abilities in JS I had to google to find some answers that points to: W3C on send/get
How would I change my code to avoid using the "false" statement?
The XML would look like:
<scoreresults>
<topscorePicture>100</topscorePicture>
<topscoreFeature>100</topscoreFeature>
<topscoreUI>100</topscoreUI>
<item>
<title>
<![CDATA[ Test sample 1 ]]>
</title>
<link> Somelink </link>
<guid isPermaLink="true"> Somelink </guid>
<scorePicture>82</scorePicture>
<scoreFeature>90</scoreFeature>
<scoreUI>85</scoreUI>
<measurePowerOTB>180</measurePowerOTB>
<measurePowerCAL>160</measurePowerCAL>
<measurePowerSTANDBY>0.2</measurePowerSTANDBY>
<measureContrastOTB>2000</measureContrastOTB>
<measureContrastCAL>2200</measureContrastCAL>
<measureBrightnessOTB>210</measureBrightnessOTB>
<measureBrightnessCAL>130</measureBrightnessCAL>
</item>
</scoreresults>
The script reads:
function loadXMLDoc(filename)
{
if (window.XMLHttpRequest)
{
xhttp=new XMLHttpRequest();
}
else // code for IE5 and IE6
{
xhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xhttp.open("GET",filename,false);
xhttp.send();
return xhttp.responseXML;
}
function showTVscore(testNo) {
//Load the XML
xmlDoc = loadXMLDoc("/testscores.xml");
//Extract the highest score for picture
tsp = xmlDoc.getElementsByTagName("topscorePicture")[0];
topspvalue = tsp.childNodes[0];
//Extract highest score for features
tsf = xmlDoc.getElementsByTagName("topscoreFeature")[0];
topsfvalue = tsf.childNodes[0];
//Extract highest score for UI
tsui = xmlDoc.getElementsByTagName("topscoreUI")[0];
topsuivalue = tsui.childNodes[0];
//Extract picture score for the specific test no.
sp = xmlDoc.getElementsByTagName("scorePicture")[testNo];
spvalue = sp.childNodes[0];
//Extract Feature score for the specific test no.
sf = xmlDoc.getElementsByTagName("scoreFeature")[testNo];
sfvalue = sf.childNodes[0];
//Extract UI score for the specific test no.
sui = xmlDoc.getElementsByTagName("scoreUI")[testNo];
suivalue = sui.childNodes[0];
//Calculate current scores
scorePicture = Math.round(Number(spvalue.nodeValue) / Number(topspvalue.nodeValue) * 100);
scoreFeature = Math.round(Number(sfvalue.nodeValue) / Number(topsfvalue.nodeValue) * 100);
scoreUI = Math.round(Number(suivalue.nodeValue) / Number(topsuivalue.nodeValue) * 100);
scoreTotal = Math.round(0.5 * scorePicture + 0.25 * scoreFeature + 0.25 * scoreUI);
//Set color of Picture scorebar
switch (Math.round(scorePicture / 10)) {
case 1:
pictureColor = "#934F00";
break;
case 2:
pictureColor = "#C36900";
break;
case 3:
pictureColor = "#F28200";
break;
case 4:
pictureColor = "C38D00";
break;
case 5:
pictureColor = "#F2AF00";
break;
case 6:
pictureColor = "#07ABFB";
break;
case 7:
pictureColor = "#008ED3";
break;
case 8:
pictureColor = "#006699";
break;
case 9:
pictureColor = "#00537B";
break;
case 10:
pictureColor = "#003F5D";
break;
};
//Set color of Feature scorebar
switch (Math.round(scoreFeature / 10)) {
case 1:
featureColor = "#934F00";
break;
case 2:
featureColor = "#C36900";
break;
case 3:
featureColor = "#F28200";
break;
case 4:
featureColor = "C38D00";
break;
case 5:
featureColor = "#F2AF00";
break;
case 6:
featureColor = "#07ABFB";
break;
case 7:
featureColor = "#008ED3";
break;
case 8:
featureColor = "#006699";
break;
case 9:
featureColor = "#00537B";
break;
case 10:
featureColor = "#003F5D";
break;
};
//Set color of UI scorebar
switch (Math.round(scoreUI / 10)) {
case 1:
uiColor = "#934F00";
break;
case 2:
uiColor = "#C36900";
break;
case 3:
uiColor = "#F28200";
break;
case 4:
uiColor = "C38D00";
break;
case 5:
uiColor = "#F2AF00";
break;
case 6:
uiColor = "#07ABFB";
break;
case 7:
uiColor = "#008ED3";
break;
case 8:
uiColor = "#006699";
break;
case 9:
uiColor = "#00537B";
break;
case 10:
uiColor = "#003F5D";
break;
};
//Set color of total scorebar
switch (Math.round(scoreTotal / 10)) {
case 1:
totalColor = "#934F00";
break;
case 2:
totalColor = "#C36900";
break;
case 3:
totalColor = "#F28200";
break;
case 4:
totalColor = "C38D00";
break;
case 5:
totalColor = "#F2AF00";
break;
case 6:
totalColor = "#07ABFB";
break;
case 7:
totalColor = "#008ED3";
break;
case 8:
totalColor = "#006699";
break;
case 9:
totalColor = "#00537B";
break;
case 10:
totalColor = "#003F5D";
break;
};
//Construct HTML
HTMLchunkStart = "<div style=\"float: Right; text-align: right; width: 40px; font-weight: 500; padding-left: 20px; color: #666;\"><i class=\"fa fa-picture-o\"></i></div><div class=\"progress\" style=\"background-color:#E2E2E2\">";
pictureBar = "<div class=\"progress-bar\" role=\"progressbar\" aria-valuenow=\"" + Number(spvalue.nodeValue) + "\" aria-valuemin=\"0\" aria-valuemax=\"" + Number(topspvalue.nodeValue) + "\" style=\"width:" + scorePicture + "%; background-color:" + pictureColor + ";\">" + scorePicture + "%</div>";
HTMLchunk1 = "</div> <div style=\"float: Right; text-align: right; width: 40px; font-weight: 500; padding-left: 20px; color: #666;\"><i class=\"fa fa-cogs\"></i></div><div class=\"progress\" style=\"background-color:#E2E2E2\">";
featureBar = "<div class=\"progress-bar\" role=\"progressbar\" aria-valuenow=\"" + Number(sfvalue.nodeValue) + "\" aria-valuemin=\"0\" aria-valuemax=\"" + Number(topsfvalue.nodeValue) + "\" style=\"width:" + scoreFeature + "%; background-color:" + featureColor + "\">" + scoreFeature + "%</div>";
HTMLchunk2 = " </div><div style=\"float: Right; text-align: right; width: 40px; font-weight: 500; padding-left: 20px; color: #666;\"><i class=\"fa fa-user\"></i></div><div class=\"progress\" style=\"background-color:#E2E2E2\">";
UIbar = "<div class=\"progress-bar\" role=\"progressbar\" aria-valuenow=\"" + Number(suivalue.nodeValue) + "\" aria-valuemin=\"0\" aria-valuemax=\"" + Number(topsuivalue.nodeValue) + "\" style=\"width:" + scoreUI + "%; background-color:" + uiColor + ";\">" + scoreUI + "%</div>";
HTMLchunk3 = "</div> <div style=\"padding-top: 3px;float: Right; text-align: right; width: 40px; font-weight: 500; padding-left: 20px; color: #666;\"><i class=\"fa fa-star\"></i></div><div class=\"progress progress-lg\" style=\"background-color:#E2E2E2;height: 30px;\">";
totalBar = "<div class=\"progress-bar progress-bar-info\" role=\"progressbar\" aria-valuenow=\"" + scoreTotal + "\" aria-valuemin=\"0\" aria-valuemax=\"100\" style=\"width:" + scoreTotal + "%; background-color:" + totalColor + ";font-size:1.1em;font-weight:600;\">Total: " + scoreTotal + "%</div></div>";
//Write the HTML
document.write(HTMLchunkStart + pictureBar + HTMLchunk1 + featureBar + HTMLchunk2 + UIbar + HTMLchunk3 + totalBar);
}