views:

66

answers:

4

I'm very interested in developing Ajax based applications. This is, loading almost all of the content of the application via XMLHttpRequest, instead of only some combos and widgets. But if I try to do this form scratch, soon I find some problems without an easy solution. I wonder if there is some framework (both client and server side) to deal with this issues. As far as I know, there isn't (but I've searched mainly in Java world). So I am seriously thinking of doing my own framework, at least for my projects.

Therefore, in this question I ask for several things. First, the possible problems of an ajax based development. Then, I'm looking for some framework or utility in order to deal with them. Finally, if there is no framework available, what features must it have.

Here are the issues I thought:

1 - JavaScript must be enabled. Security paranoia isn't the only problem: a lot of mobile devices couldn't use the application, too.

2 - Sometimes you need to update more than one DIV (e.g. main content, menu and breadcrumbs).

3 - Unknown response type: when you make an Ajax call, you set the callback function too, usually specifying if expected response is a javascript object or in which DIV put the result. But this fails when you get another type of response: for example when the session has expired and the user must log in again.

4 - Browser's refresh, back and forward buttons can be a real pain. User will expect different behaviors depending on the situation.

5 - When search engines indexes a site, only follow links. Thus, content load by Ajax won't "exist" for who doesn't know about it yet.

6 - Users can ask for open a link in a different window/tab.

7 - Address bar doesn't show the "real" page you are in. So, you can't copy the location and send it to a friend or bookmark the page.

8 - If you want to monetize the site, you can put some advertisings. As you don't refresh entire page and you want to change the ad after some time, you have to refresh only the DIV where the ad is. But this can violate the Terms and Conditions of your ad service. In fact, it can go against AdSense TOS.

9 - When you refresh an entire page, all JavaScript gets "cleaned". But in Ajax calls, all JavaScript objects will remain.

10 - You can't easily change your CSS properties.

A: 

Page content shouldn't be loaded with AJAX, for exactly those usability issues you outlined. Use PHP include() or some similar server-side templating instead.

Delan Azabani
+1  A: 

I think you have to differentiate between two scenarios:

  1. The web application is a real application for logged in users (for example, blog authors and employees...). AJAX as the main technology is fine here, as you can expect your users to fulfill your system requirements (decent browser, javascript).
  2. The web application is a normal website publicly visible to anyone. In this case, AJAX as the main technology is totally NOT fine as you loose visitors, search engines etc. Always code websites that work without the fancy stuff, then enhance the website with optional fancy things.

So, assuming your application is of type 1, your questions 1, 5 and 8 are already answered.

Question 2: no problem. The javascript controlling the AJAX call can do whatever it wants with the answer. Explode it in pieces and stick them in DIVs how you feel.

Question 3: same as 2: simply care for these cases in your javascript.

Next to questions 4, 6 and 7: you can reproduce a lot of the normal back / forward behaviour by saving the applications state into the hash of the actual url. You can change the hash without reloading the page. Many javascript frameworks offer functions for making this even more comfortable (check out dojo's history, for example).

Question 9: that is correct, but should usually be no big problem, unless your scripts are flawed.

Question 10: you can! The power of javascript can help you with that, young padawan. For example, insert link tags via javascript.

Steffen Müller
+1  A: 
  1. http://icant.co.uk/articles/pragmatic-progressive-enhancement/#build
  2. Yes. This is why you follow an MVC pattern and don't just return a chunk of m arkup
  3. See 2.
  4. http://developer.yahoo.com/yui/history/ or similar
  5. See 1.
  6. See 1.
  7. See 4, but this is imperfect and why Ajax is OK for web applications but bad for web sites
  8. You need to pick an ad service that you can work with
  9. See 4.
  10. Why not?
David Dorward
A: 

I wonder if there is some framework (both client and server side) to deal with this issues. As far as I know, there isn't

You're kidding! There's a squillion Java Ajax frameworks:

Top of the list of a google for "java ajax framework" was:

http://ajaxpatterns.org/Java_Ajax_Frameworks

Nick Pierpoint