Forum Moderators: open
function sendGet(url){
if (window.XMLHttpRequest){
xmlhttp=new XMLHttpRequest();
}else{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function(){
if(xmlhttp.readyState == 4){
if(xmlhttp.status == 200){
document.getElementById('foo').innerHTML = xmlhttp.responseText;
//WORKS FINE
return xmlhttp.responseText; //RETURNS "UNDEFINED"
}else{
alert('There was a problem with the request.');
}
}
}
xmlhttp.open("GET",url,true);
xmlhttp.send();
}
- But I don't need this info from the server to be written in a DOM object, I must use it in my JS code. The question is - how can I do that?
//onclick="foo();"
function foo() {
if (sendGet('SOMEURL')) {
// User is logged in... do stuff
loggedin();
}
else {
// User is not logged in...
notloggedin();
}
}
xmlhttp.onreadystatechange = function(){
if(xmlhttp.readyState == 4){
if(xmlhttp.status == 200){
if (xmlhttp.responseText) {
loggedin();
}
else {
notloggedin();
}
}else{
alert('There was a problem with the request.');
}
}
}
//onclick="foo();"
function foo() {
sendGet('SOMEURL');
}