Is it a good practice to have JavaScript (jQuery) code inside an Ajax response.
It works, but I feel it is not the best way (and I feel it is a bad design and makes things difficult to debug).
Should all the JavaScript be loaded before the Ajax call to the server is made?
What could be the best way to go about it? A simple example would help!
Javascript code inside AJAX response has a number of issues:
1) Javascript code loading time can not be determined. Don’t load js code dynamically from the server. script load property is not well implemented across browsers.
2) Dynamic CSS load/apply on dynamically loaded js can not be synchronous in time, again timing issue.
3) Closure issue- generally response function is child(callback) of $.ajax function, again $.ajax is child of event handler. So better to put js first time of browser load only. You may face closure issues if variables are not properly placed.
So, basically put only response parsing code in function(response){}, like XML,JSON,Text,HTML…
And remember filling heavy code inside callback function is a bad practice.