Is it possible to retrieve variable set in onreadystatechange function from outside the function?
–edit–
Regarding execution of functions:
If its possible i would like to execute ajaxFunction() with one click
and then popup() with next click, or somehow wait for ajax function to end and then call for alert box
In pseudocode:
function ajaxFunction(){ //creating AJAX ... // Create a function that will receive data sent from the server ajaxRequest.onreadystatechange = function (){ if(ajaxRequest.readyState == 4){ //success code ======>Here i want to set variable <===== var MyVariable='MyContent'; } } //Retrieving page .... } function popup(){ ajaxFunction(); alert(MyVariable); }
The following code assumes that the ajax-request is synchronous:
But since synchronous requests are blocking the browser you should in almost all cases use asynchronous calls (If I remember correctly onreadystatechange should not be called on synchronous request but different browsers behaves differently)
What you could do is: