Forum Moderators: open

Message Too Old, No Replies

Load order for ajax?

         

music_man

7:27 am on Mar 10, 2007 (gmt 0)

10+ Year Member



Hi

I am trying to get the result of an ajax function into a variable.

The problem is, it loads the ajax after the function that gets the variable, so the variable is undefined.

Here is my code:


<script type="text/javascript">

/* ajax */
function startAjax(obj)
{
var obj;
try{
ajaxRequest=new XMLHttpRequest()}
catch(e){
try{
ajaxRequest=new ActiveXObject("Msxml2.XMLHTTP")}
catch(e){
try{
ajaxRequest=new ActiveXObject("Microsoft.XMLHTTP")}
catch(e){
alert("Your browser does not appear to support this application.");
return false}
}
}
return obj}

function doAjax(action) {
startAjax('ajaxResponse');
var uid = Math.random();
var str = 'a='+action;
str +="&uid="+uid;
var url = "index.php";
ajaxRequest.open("POST",url,true);
ajaxRequest.setRequestHeader("Content-Type",
"application/x-www-form-urlencoded; charset=UTF-8");
ajaxRequest.send(str);
ajaxRequest.onreadystatechange = function() {
if (ajaxRequest.readyState == 4) {
alert('loaded');
var a1;
a1 = escape(ajaxRequest.responseText);
return a1;
// Doesn't return variable!

}
}
}
function makeElem(elem,id,text,dElem) {
var dElem = document.getElementById(dElem);
var e1 = document.createElement(elem);
e1.setAttribute('id',id);
var r1 = document.createTextNode(text);
e1.appendChild(r1);
dElem.appendChild(e1);
}
function startChat(welcome) {
setupChatWindow(welcome);// setup chat window
setupChattersWindow();
refreshChatters();// setup chatters window
}
function setupChatWindow(welcome) {
makeElem('div','title',welcome,'cw');
}
function setupChattersWindow() {
makeElem('div','title','Chatters','chw');// Make header
}
function joinChat() {

refreshChatters();
}
// This is the problem one

function refreshChatters() {
var a1 = doAjax('refreshChatters');
alert(a1);
}
</script>

mehh

2:05 pm on Mar 10, 2007 (gmt 0)

10+ Year Member



try this

function doAjax(action) {
startAjax('ajaxResponse');
var uid = Math.random();
var str = 'a='+action;
str +="&uid="+uid;
var url = "index.php";
ajaxRequest.open("POST",url,false);
ajaxRequest.setRequestHeader("Content-Type",
"application/x-www-form-urlencoded; charset=UTF-8");
ajaxRequest.send(str);
alert('loaded');
var a1;
a1 = escape(ajaxRequest.responseText);
return a1;
alert('a1='+a1)
}