javascript - Fix JS XML load to not use async=false - how? -
i set out xml parse (single node) using jquery, had no luck ended using plain javascript instead. code found in thread: parse xml using js
now can see way use xmlhttprequest function deprecated , might removed in future, ruin quite lot on homepage xmlhttprequest , google inspector reports
synchronous xmlhttprequest outside of workers in process of being removed web platform has detrimental effects end user's experience
i re-open original question, , rewriting below load xml without using async=false in code
xhttp.open("get",filename,false);
the code being invoked 9/10 down in hmtl-code, changing "false" "true" no good, nothing loads @ (i guess code moves on before has chance insert xml-data?). weak abilities in js had google find answers points to: w3c on send/get
how change code avoid using "false" statement?
the xml 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 ie5 , ie6 { xhttp=new activexobject("microsoft.xmlhttp"); } xhttp.open("get",filename,false); xhttp.send(); return xhttp.responsexml; } function showtvscore(testno) { //load xml xmldoc = loadxmldoc("/testscores.xml"); //extract highest score picture tsp = xmldoc.getelementsbytagname("topscorepicture")[0]; topspvalue = tsp.childnodes[0]; //extract highest score features tsf = xmldoc.getelementsbytagname("topscorefeature")[0]; topsfvalue = tsf.childnodes[0]; //extract highest score ui tsui = xmldoc.getelementsbytagname("topscoreui")[0]; topsuivalue = tsui.childnodes[0]; //extract picture score specific test no. sp = xmldoc.getelementsbytagname("scorepicture")[testno]; spvalue = sp.childnodes[0]; //extract feature score specific test no. sf = xmldoc.getelementsbytagname("scorefeature")[testno]; sfvalue = sf.childnodes[0]; //extract ui score 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 html document.write(htmlchunkstart + picturebar + htmlchunk1 + featurebar + htmlchunk2 + uibar + htmlchunk3 + totalbar); }
instead of returning value loadxmldoc
, pass callback function.
add readystatechange
event handler and, when have loaded data, call function , pass argument.
function loadxmldoc(filename, callback) { var xhr = new xmlhttprequest(); xhr.open("get", filename); xhr.onreadystatechange = function () { if (this.readystate === 4 && this.status === 200) { callback(this.responsexml); } }; xhr.send(); }
Comments
Post a Comment