1

Declare global variables(jquery):

 $.mynamespace = {  myVar : "something" }; 

I will do some operations with xml, after that will assign some other value to myVar and call to function test() will again change the value of myVar. Then I want the value of var must be same as I changed in test() function.

    $(document).ready(function(){
        //Some XML oprations
        $.get("students.xml",{},function(xml){
            $.mynamespace.myVar="ewewewewewew";
            test();
        });
        //value of $.mynamespace.myVar must be test
        alert($.mynamespace.myVar);
     });

     function test(){
        alert($.mynamespace.myVar );
         $.mynamespace.myVar="test";
     }
BenMorel
  • 34,448
  • 50
  • 182
  • 322
Ganesh
  • 505
  • 2
  • 9
  • 26
  • possible duplicate of [How to return the response from an AJAX call?](http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-ajax-call) – adeneo Jul 18 '13 at 07:03
  • Maybe try to put this code at the top of your code : var $.mynamespace = {} – Lucas Willems Jul 18 '13 at 07:04
  • 2
    `$.get` is async, the issue isn't wether or not the variable is global, but that the ajax call hasn't completed when you are trying to use the variable. – adeneo Jul 18 '13 at 07:04
  • "value of $.mynamespace.myVar must be test" - it will be after the ajax request is completed, but until then the alert(which is executed immediately after the call) will show the current value. – undefined Jul 18 '13 at 07:05
  • It is async so it can come anytime. That's whay you should something with this variable in function(xml) or use attribute Sync but when you use Sync then it's not AJAX anymore :) – Robert Jul 18 '13 at 07:06
  • check this example http://jsfiddle.net/KrXZy/ – Kathiravan Jul 18 '13 at 07:08
  • adeneo:then in that case in need to make async:flase,thanks for point out that. – Ganesh Jul 18 '13 at 08:03

2 Answers2

1

Ajax stands as Asynchronous JavaScript and XML which means that the calls are Asynchronous. When AJAX is done the sucessful function is called. It can be called anytime. So when javascript reaches the code it just goes through and when ajax is ready success function is called.

There are 2 solutions.

  1. You do something with variable when ajax request is done in sucessful function
  2. You do something with variable after the ajax code but you need to use param async: false which makes Ajax NO ajax :) but some kind of SJAX.
Robert
  • 19,800
  • 5
  • 55
  • 85
0

You can use closures instead of global vars to do the same thing

(function($){
  var myNs = {};

  $(document).ready(function(){ 

    //Some XML oprations
    $.get("students.xml",{async: false},function(xml){
        myNs.myVar="ewewewewewew";
        test();
    });
        //value of $.mynamespace.myVar must be test
        alert(myNs.myVar);
  });

     function test(){
        alert(myNs.myVar );
         myNs.myVar="test";
     }
})(jQuery);
krampstudio
  • 3,519
  • 2
  • 43
  • 63