i making ajax call inline below , storing response in global variable , accessing later performance reasons.
<script type="text/javascript"> var gblajaxresponse; $.ajax({ url: '/test.aspx', success: function(data) { gblajaxresponse=data; } }); </script>
now want use global variable in document.ready of ajax file. once dom ready , have use data on divs on page.
$(document).ready(function() { alert(gblajaxresponse); });
i have following questions now.
1) ajax asyncrnous call sure response global variable time ready document.ready. 2) there chances document.ready code gets executed below ajax success handler returns 3) don't want use asyn false option can hang. 4) there anyway check if success completed , returned data.
any suggestions appreciated
1) ajax asyncrnous call sure response global variable time ready document.ready.
no, document.ready
can executed before.
2) there chances document.ready code gets executed below ajax success handler returns
yes
4) there anyway check if success completed , returned data.
basing on kennis' answer, this:
<script type="text/javascript"> var gblajaxresponse; $.ajax({ url: '/test.aspx', success: function(data) { gblajaxresponse = data; mycallbackfunction(); } }); function mycallbackfunction(){ alert(gblajaxresponse); } </script>
if need use gblajaxresponse
in document.ready
, too:
<script type="text/javascript"> var gblajaxresponse = null; $.ajax({ url: '/test.aspx', success: function(data) { gblajaxresponse = data; } }); $(document).ready(function(){ //wait until gblajaxresponse has value: var intervalid = setinterval(function(){ if(gblajaxresponse !== null){ clearinterval(intervalid); mycallbackfunction(); } }, 100); }); function mycallbackfunction(){ alert(gblajaxresponse); } </script>
Comments
Post a Comment