variable value and execution order

M

Mariano

I have this function (ajaxRequest), inside this function there is an
anonymous function that call a new function (alteraDomanda).
alteraDomanda is a simple function that return a string value when
executed. I woould that this string could be readed out of the
anonymous function, exactly here:
alert("2: "+post_param);
XMLHttpRequestObject.send(post_param);
But, although i have defined post_param out of all functions, previous
code return me 2: undefined.

Another strang thing is that is alway executed alert("2:
"+post_param); for first, and after alert("1: "+post_param). The first
alert return me "2: undefined", the second alert "1: IN FUNCTION".

I would that all alert will read and print "IN FUNCTION", and... why
alert("2: "+post_param); is executed first of alert("1:
"+post_param);???

var post_param;
function ajaxRequest(func, src, type, p1, p2, p3, p4, p5, p6, p7) {
var XMLHttpRequestObject = false;

if (window.XMLHttpRequest) {
XMLHttpRequestObject = new XMLHttpRequest();
} else if (window.ActiveXObject) {
XMLHttpRequestObject = new ActiveXObject("Microsoft.XMLHTTP");
}

if(XMLHttpRequestObject) {
XMLHttpRequestObject.open(type, src, true);

if (type=='POST')
XMLHttpRequestObject.setRequestHeader('Content-Type',
'application/x-www-form-urlencoded');

XMLHttpRequestObject.onreadystatechange = function(){
if (XMLHttpRequestObject.readyState == 1) {
// PRELOADER
} else if (XMLHttpRequestObject.readyState == 4 &&
XMLHttpRequestObject.status == 200) {
var xmlDocument = XMLHttpRequestObject.responseXML;

post_param = alteraDomanda();
alert("1: "+post_param);
}
}

if (type=='GET')
XMLHttpRequestObject.send(null);
else if (type=='POST'){
alert("2: "+post_param);
XMLHttpRequestObject.send(post_param);
}
}
}

function alteraDomanda() {
return "IN FUNCTION";
}
 
D

David Mark

I have this function (ajaxRequest), inside this function there is an
anonymous function that call a new function (alteraDomanda).
alteraDomanda is  a simple function that return a string value when
executed. I woould that this string could be readed out of the
anonymous function, exactly here:
            alert("2: "+post_param);
            XMLHttpRequestObject.send(post_param);
But, although i have defined post_param out of all functions, previous
code return me 2: undefined.

Another strang thing is that is alway executed alert("2:
"+post_param); for first, and after alert("1: "+post_param). The first
alert return me "2: undefined", the second alert "1: IN FUNCTION".

I would that all alert will read and print "IN FUNCTION", and... why
alert("2: "+post_param); is executed first of alert("1:
"+post_param);???

var post_param;
function ajaxRequest(func, src, type, p1, p2, p3, p4, p5, p6, p7) {
    var XMLHttpRequestObject = false;

    if (window.XMLHttpRequest) {
        XMLHttpRequestObject = new XMLHttpRequest();
    } else if (window.ActiveXObject) {
        XMLHttpRequestObject = new ActiveXObject("Microsoft.XMLHTTP");
    }

    if(XMLHttpRequestObject) {
        XMLHttpRequestObject.open(type, src, true);

        if (type=='POST')
            XMLHttpRequestObject.setRequestHeader('Content-Type',
'application/x-www-form-urlencoded');

        XMLHttpRequestObject.onreadystatechange = function(){
            if (XMLHttpRequestObject.readyState == 1) {
                // PRELOADER
            } else if (XMLHttpRequestObject.readyState == 4 &&
XMLHttpRequestObject.status == 200) {
                var xmlDocument = XMLHttpRequestObject.responseXML;

                  post_param = alteraDomanda();
                  alert("1: "+post_param);
            }
        }

This code is not executed until the readystate changes and only after
the outer function has exited (in the case of asynchronous requests.)
        if (type=='GET')
            XMLHttpRequestObject.send(null);
        else if (type=='POST'){
            alert("2: "+post_param);

So, post_param is undefined at this point and this alert will occur
before the other.

[snip]
 
M

Mariano

This code is not executed until the readystate changes and only after
the outer function has exited (in the case of asynchronous requests.)




So, post_param is undefined at this point and this alert will occur
before the other.


So what can I do to avoid the undefined value and obtain the value
returned from alteraDomanda that is: "2: IN FUNCTION". Don't import
exactly the order, only avoiding undefined....
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top