0

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!

1
  • If you can give us an example of some script you load with an ajax response, we can give you an example of how to avoid it. Commented Dec 11, 2011 at 18:25

3 Answers 3

4

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.

Sign up to request clarification or add additional context in comments.

3 Comments

"script load property is not well implemented across browsers" Between onload and onreadystatechange on the script element, all major and most minor browsers are covered. Still not saying it's a good idea, though. :-)
Thanks for info, actually i faced some trouble earlier with load, so pointed.but we can't say when script is loaded and executed, so that we can proceed further.
@Praveen: That's my point: With all major, and most minor, browsers, we can. If you watch for the load event and the readystate event (watching for the right readyState), you will get a notification. By that time, the code has run.
2

I think your engineering instincts are right on the money. Put your code in a JavaScript file, compress/minify/pack it, and serve it once. The browser can then cache it. Limit your ajax calls to retrieving data or markup from the server, rather than code.

...95% of the time. I'd be willing to bet you could justify the odd bit of code-as-payload sometimes, but I haven't had to do it yet.

5 Comments

And what about the time when you need to use JSONP?
@Tadeck: JSONP is data. :-) The code involved in purely to work around the SOP.
Actually I disagree :) JSONP is actually JS script that calls some function. Simple script, but it is still a script. Yes, it is meant to pass some data and yes, it is SOP workaround, but it is still a script. Even the article I referenced says that JSONP is about retrieving JS code, which is then parsed by JS interpreter, not JSON parser. Do you agree?
@Tadeck: The difference between JSON and JSONP consists purely of the putting someFunction( in front of the JSON and ); after it (and, of course, the difference in how the consumer requests it -- e.g., ajax [typically] in the JSON case, a script element in the JSONP case). So the JSONP text is technically code, but functionally it's data.
So basically what you are saying, the difference between JSON and JSONP consists purely of changing JSON string into JS code. And yes, technically this is JavaScript code - that is what I asked. Thanks, I have no other questions. I also agree that sending JS code dynamically without really good reason (such as using JSONP as a workaround for SOP) is not a good practice (if that is what you are saying).
0

I think it's okay to have a little, but not a lot, of script code load along with an ajax response.

For example, we use MVC3 and its jquery unobtrusive validation plugin. Some of our forms have partial views loaded via ajax. After they load, in order to get the client-side validation to work again, we have to run this one line of jQuery:

$.validator.unobtrusive.parse('form');

Something like that I don't mind having in the partial view, though it could just as easily come in the $.ajax.success function.

2 Comments

When you have complex application, then debugging such code becomes also more complex. Also OP asked about a good practice, not whether we make some exceptions.
I don't really agree (with @olivehour), I have never seen an application where such kind of dynamic behaviour is really necessary. It just opens the gate towards silly programming.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.