In the change_text() function I am trying to pass it the xmhttp.responsetext variable.. but how will I do this? As I see no way yet on how to pass it?
<script type="text/javascript">
function ajax(url, func) {
this.url = url;
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if(xmlhttp.readyState == 2 && xmlhttp.status == 200) {
this.func = func;
}
}
xmlhttp.open();
xmlhttp.send()
}
function change_text() {
target = document.getElementById("x");
target.innerHTML = xmlhttp.responseText;
}
ajax("url.php", change_text);
</script>
Actually, to do this you don’t want to use
thisat all, since you are not creating any instances of an object and it doesn’t act like a constructor.You can just:
That would make the
ajaxfunction work.For your
change_textfunction, it’s not in the same function asajax, so it does not have access to thexmlhttpvariable. However, theajaxfunction passed the responseText to it, so just make your function receive it:For a working example, see the jsFiddle.