0

I'm trying to write a Javascript/jQuery function that loads another page from a server, parses it (to determine its parent page), and returns that value.

My function currently looks like this:

function getParent(pageNo) {
    $.get( [page_reference] , function(loaded_stuff) {

    [process to parse loaded_stuff to determine return_value]

    alert (return_value);  // displays the value I want
    });
}

But I can't figure out how to capture the return value.

If I put a return statement directly after the alert(), I'm returning to some JQuery function, not the function I'm calling from. My function receives undefined.

If I put a return statement directly before the final curly bracket, then Javascript will execute it before it finishes the asynchronous AJAX bit. My function receives the initial value of return_value.

I'm sure there must be a simple answer to this, but I've been unable to find it. I can't figure out how to make the functions execute sequentially. Can you help me?

syockit
  • 5,747
  • 1
  • 24
  • 33
Mike K
  • 486
  • 3
  • 7
  • try this link [http://stackoverflow.com/questions/6861039/jquery-function-return-value][1] – morne Jun 18 '14 at 05:52
  • asynchronous call means it requests for it and goes with next line, doesnt wait for it to complete, so putting the return statement before the last curly braces( end of function) will not work – dreamweiver Jun 18 '14 at 05:53

2 Answers2

0

val returnedValue=return_value;

and then you can do whatever you want with it!

UPDATE:

function getParent(pageNo) {
    $.get( [page_reference] , function(loaded_stuff) {

    [process to parse loaded_stuff to determine return_value]

    alert (return_value);  // if return_value returns the desired value then you should just return that like this
    return return_value;
    //or store it in a variable and then use it as you like
    val returnedValue=return_value;  
    //note that if you choose to store it in a variable then you must remove the "return return_value;" line
    });
}
Amin Jafari
  • 7,157
  • 2
  • 18
  • 43
  • is this what you want? – Amin Jafari Jun 18 '14 at 05:58
  • I'm sorry, I need more details - where do I put it, and what does it do? Is 'returnedValue' a new variable, or a predefined property of something? – Mike K Jun 18 '14 at 06:06
  • I don't think that's the answer. I've tried 'return'ing directly from the function, and it doesn't work. And setting another variable is OK, but it contorts the rest of the code (because I can't just call the function as a value). – Mike K Jun 18 '14 at 06:16
  • you mean return_value is a function? if so you can use returnedValue() – Amin Jafari Jun 18 '14 at 06:19
0

Since ajax is async it will not work, you will have to work your way around it with doing the work in the callback.

<script>
    function getParent(pageNo) {
        $.get( [page_reference] , function(loaded_stuff) {

        [process to parse loaded_stuff to determine return_value]

        alert (return_value);  // displays the value I want
        ActuallyDoTheWork(return_value);
        });
    }

    function ActuallyDoTheWork(return_value)
    {
        //do the updating work here
    }
</script>
Jens
  • 467
  • 1
  • 5
  • 13
  • Thank you. I think that's clarified what I've been reading elsewhere for my specific context. – Mike K Jun 18 '14 at 06:13