I have this code:
var temp = getLevel(0);
console.log("outside" + temp + "/outside");
function getLevel(level) {
var httpRequest, lvlCode;
if (window.XMLHttpRequest) httpRequest = new XMLHttpRequest(); // most browsers
if (window.ActiveXObject) httpRequest = new ActiveXObject("Microsoft.XMLHTTP"); // IE8 and older
httpRequest.open('GET', 'get-level.php', true);
httpRequest.onreadystatechange = function() {
if (httpRequest.readyState === 4) {
lvlCode = httpRequest.responseText;
console.log("inside" + lvlCode + "/inside");
return lvlCode;
}
};
httpRequest.send("lvl=" + level);
}
Console returns:
outsideundefinedoutside (index):111
inside<data I want>
<!-- Hosting24 Analytics Code -->
<script type="text/javascript" src="http://stats.hosting24.com/count.php"></script>
<!-- End Of Analytics Code -->
/inside
There are two issues here: first, Hosting24 pollutes my echoed data. How do I get rid of the Hosting24 text? Second, why is temp
undefined when it should contain exactly the same as lvlCode?