I am making an ajax call inline as below and storing the response in global variable and accessing later for some performance reasons.
<script type="text/javascript">
var gblAjaxResponse;
$.ajax({
url: '/test.aspx',
success: function(data) {
gblAjaxResponse=data;
}
});
</script>
Now i want to use this global variable in document.ready of an ajax file. Once the DOM is ready , i have to use this data on some divs on that page.
$(document).ready(function() {
alert(gblAjaxResponse);
});
I have the following questions now.
1) As ajax is asyncrnous call is it sure that i always get the response into that global variable by the time i ready document.ready.
2) Are there any chances that my document.ready code gets executed below the ajax success handler returns
3) I don’t want to use asyn false option as it can hang.
4) Is there anyway to check if success is completed and returned data.
any suggestions will be appreciated
No,
document.readycan be executed before.Yes
Basing on kennis’ answer, you could do this:
If you need to use
gblAjaxResponseindocument.ready, you could do this too: