Friday, May 17, 2024
 Popular · Latest · Hot · Upcoming
164
rated 0 times [  165] [ 1]  / answers: 1 / hits: 58137  / 13 Years ago, fri, march 18, 2011, 12:00:00

In my application, I am using Ext.Ajax.request to load scripts which I execute with eval.



The problem is that since it takes time for the AJAX request to complete, code that is executed afterward which needs variables which are in the script loaded in via AJAX. In this example, I show how this is the case. How can I change this code so that the execution of the JavaScript after the AJAX waits until the script in the AJAX call has been loaded and executed?



testEvalIssue_script.htm:



<script type=text/javascript>
console.log('2. inside the ajax-loaded script');
</script>


main.htm:



<html>
<head>
<script type=text/javascript src=ext/adapter/ext/ext-base.js></script>
<script type=text/javascript src=ext/ext-all-debug.js></script>
<script type=text/javascript>
function loadViewViaAjax(url) {
Ext.Ajax.request({
url: url,
success: function(objServerResponse) {
var responseText = objServerResponse.responseText;
var scripts, scriptsFinder=/<script[^>]*>([sS]+)</script>/gi;
while(scripts=scriptsFinder.exec(responseText)) {
eval.call(window,scripts[1]);
}
}
});
}

console.log('1. before loading ajax script');
loadViewViaAjax('testEvalIssue_script.htm');
console.log('3. after loading ajax script');
</script>
</head>
<body>

</body>

</html>


output:



1. before loading ajax script
3. after loading ajax script
2. inside the ajax-loaded script


How can I get the output to be in the correct order, like this:



1. before loading ajax script
2. inside the ajax-loaded script
3. after loading ajax script

More From » ajax

 Answers
51

Ajax is asynchronous, that means that the ajax call is dispatched but your code keeps on running as happy as before without stopping. Ajax doesn't stop/pause execution until a response is received. You'll have to add an extra callback function or something like that.



<html>
<head>
<script type=text/javascript src=ext/adapter/ext/ext-base.js></script>
<script type=text/javascript src=ext/ext-all-debug.js></script>
<script type=text/javascript>
function loadViewViaAjax(url, callback) {
Ext.Ajax.request({
url: url,
success: function(objServerResponse) {
var responseText = objServerResponse.responseText;
var scripts, scriptsFinder=/<script[^>]*>([sS]+)</script>/gi;
while(scripts=scriptsFinder.exec(responseText)) {
eval.call(window,scripts[1]);
}
callback.call();
}
});
}

console.log('1. before loading ajax script');
var afterAjax = function(){
console.log('3. after loading ajax script');
}
loadViewViaAjax('testEvalIssue_script.htm', afterAjax);
</script>
</head>
<body>

</body>

</html>

[#93203] Thursday, March 17, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
mustafaericho

Total Points: 322
Total Questions: 103
Total Answers: 110

Location: Montenegro
Member since Thu, Jun 16, 2022
2 Years ago
mustafaericho questions
Mon, May 31, 21, 00:00, 3 Years ago
Sun, May 23, 21, 00:00, 3 Years ago
Sat, Feb 13, 21, 00:00, 3 Years ago
Sat, Jan 2, 21, 00:00, 3 Years ago
Thu, Nov 12, 20, 00:00, 4 Years ago
;