Access jQuery variable outside its function -


i need use variable outside function. read older questions, still can't find proper solution.

here's code:

<script>  var warehouseaddress;  $(document).ready(function(){  $.ajax({     type:"post",     url:"${pagecontext.request.contextpath}/users/findwarehouseaddress",       success:function(data){         warehouseaddress = data;     } });   alert(warehouseaddress);  });  var warehouse = warehouseaddress;  </script> 

the alert gives me "undefined". need variable not inside alert, inside other "warehouse" value outside ready function.

sorry if it's duplicate, said couldn't make answers read.

edit: sake of completeness describe details, replacing alerts read functions have call.

as said in comments, i'm using google maps apis , functions. have passing variable outside $(document).ready function, because need use in external script.

the problem external js function have call google maps function (initialize()) loads window loads, have find way call function after $ready function.

as @putvande said ajax asynchronous, best thing here take advantage of callbacks ( event based :d )

$.ajax({     type:"post",     url:"${pagecontext.request.contextpath}/users/findwarehouselat",       success: mydatahandler(data) });   function mydatahandler(data){     warehouselat = data;     //do data here :d exists     alert(warehouselat); } 

have @ this: custom events in jquery


Comments

Popular posts from this blog

matlab - Deleting rows with specific rules -

php - MySQLi multi_query results for later use -