Cod-nes Posted July 17, 2010 Share Posted July 17, 2010 I want the function checkuser() to return a true/false status based on the server response, but the script returns to inter function of checkuser(). function checkuser(user) { if (window.XMLHttpRequest) { xmlhttp=new XMLHttpRequest(); } else { xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); }xmlhttp.onreadystatechange=function() { if (xmlhttp.readyState==4 && xmlhttp.status==200) { alert(xmlhttp.responseText); if(xmlhttp.responseText == 'usertaken') { alert('ifok'); return(true); } else { return(false); } } }xmlhttp.open("GET","index.php?act=isusertaken&user=" + user,true);xmlhttp.send();} Link to comment Share on other sites More sharing options...
boen_robot Posted July 17, 2010 Share Posted July 17, 2010 You'll have to rethink what you're doing. You'll have to do whatever you want to happen after the request in the onreadystatechage function. This may include calling different other functions (which may be passed as an argument to checkuser() for example), but they need to be called at onreadystatechange. Link to comment Share on other sites More sharing options...
Cod-nes Posted July 18, 2010 Author Share Posted July 18, 2010 Thanks. I have a question. Can we do a return function with the response text after xmlhttp.send() or is that not allowed? Link to comment Share on other sites More sharing options...
thescientist Posted July 18, 2010 Share Posted July 18, 2010 Thanks. I have a question. Can we do a return function with the response text after xmlhttp.send() or is that not allowed?are you asking if you can pass the response text to a function by passing it as an argument? (yes, you can) Link to comment Share on other sites More sharing options...
boen_robot Posted July 18, 2010 Share Posted July 18, 2010 are you asking if you can pass the response text to a function by passing it as an argument? (yes, you can)I don't think that's what he's asking... he's still trying to make checkuser() return.You can make the whole request process syncronous, insead of asyncronous, but this means checkuser() will block out everything during the request. To do that, just change the third argument on the open() method, like:xmlhttp.open("GET","index.php?act=isusertaken&user=" + user,false); Once that is done, you can move all of your code outside of onreadystatechange, and right after send(), like: xmlhttp.open("GET","index.php?act=isusertaken&user=" + user,true);xmlhttp.send(); if(xmlhttp.responseText == 'usertaken') { alert('ifok'); return(true); } else { return(false); } Again - this has MAJOR performance implications. It will make checkuser() take a really long time, and nothing else will be doable before that function is finished. The asyncronous approach you were using before didn't have that problem - no matter how long checkuser() takes, other JavaScript would also be active, and users would not be blocked while they wait. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.