How to Assign value to global variable and use it in other functions in jquery/javascript? -


declare global variables(jquery):

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

i operations xml, after assign other value myvar , call function test() again change value of myvar. want value of var must same changed in test() function.

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

ajax stands asynchronous javascript , xml means calls asynchronous. when ajax done sucessful function called. can called anytime. when javascript reaches code goes through , when ajax ready success function called.

there 2 solutions.

  1. you variable when ajax request done in sucessful function
  2. you variable after ajax code need use param async: false makes ajax no ajax :) kind of sjax.

Comments

Popular posts from this blog

php - Calling a template part from a post -

Firefox SVG shape not printing when it has stroke -

How to mention the localhost in android -