variable value and execution order

Discussion in 'Javascript' started by Mariano, Feb 19, 2008.

  1. Mariano

    Mariano Guest

    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";
    }
    Mariano, Feb 19, 2008
    #1
    1. Advertising

  2. Mariano

    David Mark Guest

    On Feb 19, 11:00 am, Mariano <> wrote:
    > 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]
    David Mark, Feb 19, 2008
    #2
    1. Advertising

  3. Mariano

    Mariano Guest

    On 19 Feb, 20:54, David Mark <> wrote:
    > On Feb 19, 11:00 am, Mariano <> wrote:
    >
    >
    >
    > > 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.



    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....
    Mariano, Feb 19, 2008
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. priya
    Replies:
    0
    Views:
    1,233
    priya
    Oct 3, 2005
  2. =?Utf-8?B?QmlsbCBCb3Jn?=

    Execution order of PageLoad for user controls

    =?Utf-8?B?QmlsbCBCb3Jn?=, Mar 6, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    3,345
    =?Utf-8?B?QmlsbCBCb3Jn?=
    Mar 6, 2004
  3. Ignacio Machin \( .NET/ C#  MVP \)

    order of execution of page_load in a base and derived classes

    Ignacio Machin \( .NET/ C# MVP \), Sep 1, 2004, in forum: ASP .Net
    Replies:
    13
    Views:
    893
    Ignacio Machin \( .NET/ C# MVP \)
    Sep 2, 2004
  4. Dick Brown
    Replies:
    7
    Views:
    341
    James Kanze
    Feb 3, 2009
  5. Zeguudo Zegudissimo
    Replies:
    4
    Views:
    488
    Zeguudo Zegudissimo
    Sep 2, 2010
Loading...

Share This Page