0

I am trying to pass the value of a parameter outside a $.get jQuery in javascript. I read that I should somehow do that in synchronous mode but I couldn't find a solution.

Here is my code:

var flag;
var t = $.get("mutalyzer.php?id="+variation.value, function(data) {
    flag=data;
});
document.write(flag);

where I get undefined as a result.

Thanks!

1
  • 2
    Put the document.write() inside the $.get() callback, and don't take suggestions to use synchronous ajax seriously. Commented Apr 1, 2013 at 13:37

1 Answer 1

3

write it inside the callback function

try this

var t = $.get("mutalyzer.php?id="+variation.value, function(data) {
   //this is the callback function and runs when get gets  completed
   flag=data;
   document.write(flag); 
 });
Sign up to request clarification or add additional context in comments.

3 Comments

it should be noted that calling document.write() in there will indeed show the value returned, but the net result will be that that will be all that's left of the original page.
HI, actually i want to use the variable flag in other functions in my code, not only print it.
then you need to pass that inside the callback function..... calling it outside is not a good idea since you function may get called before the ajax returns something

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.