22

I'm building a web application with the Zend Framework. I have wanted to include some AJAX type forms and modal boxes, but I also want my application to be as accessible as possible. I want my application to be enhanced by AJAX, but also fully functional without AJAX.

So as a general guideline...when should I not use AJAX? I mean, should I bother making my application usable without AJAX? Or does everyone have AJAX enabled browsers these days?

Andrew
  • 227,796
  • 193
  • 515
  • 708
  • 5
    AJAX? Maybe you are talking about "without JavaScript"? Its a good idea to make your site work for js-disabled browsers... But I think it doesn't worth it - too many effort to make a 1% users happy. And 1% is an overrated number. Im sure 0.9% is a search robots and only 0.1% is real users with text-based or smtng browser w/o js support. – NilColor Nov 25 '09 at 21:58
  • Here's a similar question: http://stackoverflow.com/questions/155615/ – Justin Johnson Nov 25 '09 at 22:58

17 Answers17

19

If you mean "accessible" in the ADA sense, AJAX is usually a no-no - your site should provide all its content and core functionality using only standard (X)HTML and CSS. Any javascript used should merely extend the core functionality, and your site should be coded to work elegantly in the absence of a javascript-enabled browser.

Examples: if you want a user to click on a thumbnail and get a full-size version of the image as a result, you can make the thumbnail a link. Then, the onclick event will fire a JQuery method that cancels the navigation behavior of the link and pops up a JQuery floating div to show the image on the current page. If the user's browser doesn't support JavaScript, the onclick event will never fire, and the user will be presented the image in a new page. The core functionality is the same with or without scripting.

EDIT: Skeleton example, sans JQuery-specific code.

<html>
<body>
<a href="some.url" onclick="JQueryToOpenPopupImage(); return false;">Some URL</a>
</body>
</html>

To cancel the navigation operation, simply make sure that the method invoked by the onclick event returns false at the end.

A neat example of the JQuery image popup I described can be found here.

Dathan
  • 7,266
  • 3
  • 27
  • 46
  • Would you mind showing some example code of how to disable the click and do something else with jQuery, like you mentioned? – Andrew Nov 25 '09 at 21:29
  • Alright, I've posted an update to my answer. – Dathan Nov 25 '09 at 21:36
  • 2
    You can actually use http://docs.jquery.com/Events/jQuery.Event#event.preventDefault.28.29 . Javascript code in onClick is harder to manage. – Maiku Mori Nov 26 '09 at 01:15
9

Use ajax if it adds value for the user.

If the ajax version adds a lot more value than the non-ajax version then it might justify the expense to develop a solution that caters for both clients. Generally i wouldn't recommend doing the extra work (remember.. more code results in more maintenance).

Matt Kocaj
  • 11,278
  • 6
  • 51
  • 79
  • 1
    The optimal amount of code is no code, so I agree with cottsak: use ajax only if it adds real value for the user. – hora Nov 26 '09 at 00:59
8

I think one point is missing here: Use Ajax only for content any search engine does not need to know.

TheHippo
  • 61,720
  • 15
  • 75
  • 100
5

98% of users will have AJAX enabled browsers.

A significant percentage of those people won't have it turned on when they first visit your site though (or at all, ever perhaps).

I've seen websites that look like a blank page without javascript on. Don't be one of them. Javascript to fix layout issues is a horrible idea in my opinion. Make sure it loads and looks ok without Javascript. If people can atleast see what they are missing out on, they are likely to switch it on, but if your website looks like it's just broken, then...

Matthew Scharley
  • 127,823
  • 52
  • 194
  • 222
  • Define "A significant percentage." According to w3schools, as of January 2008, only 5% of users didn't have JavaScript enabled http://www.w3schools.com/browsers/browsers_stats.asp and I suspect that you will see that number decrease a little this year. Also, demographic is an important thing to consider. If the OP is not targeting developers and the otherwise paranoid, chances are good that JavaScript is enabled. – Justin Johnson Nov 25 '09 at 22:55
  • 5% is a significant number in most circles. If you had to pay me 5% of your income, I'm sure you'd start complaining pretty quickly. – Matthew Scharley Nov 26 '09 at 06:28
  • As a more solid example though, only 10% of users (by the same stats) use either Chrome or Safari (~5% each), but where I work we test both those cases too. – Matthew Scharley Nov 26 '09 at 06:33
4

I often have noscript block Flash and JavaScript until I make the decision that your site is worthy.

So be sure to tell me what I'm missing if I have JavaScript turned off.

Nosredna
  • 83,000
  • 15
  • 95
  • 122
  • Would you mind showing some example code of how you detect if the user has purposefully disabled javascript? – Andrew Nov 25 '09 at 21:31
  • 1
    Since you don't have JavaScript running, you can't use JavaScript to check. Instead, take a look at the HTML noscript tag. – Nosredna Nov 25 '09 at 21:38
3

It depends on the complexity of your web application.
If you can, having it functional with javascript disabled is great, because it makes your application usable not only by users on js-disabled browsers but also by robots. The day you decide to write an application to automatically fill your forms, for example, you don't have to write an API from the ground up.

In any case, do not user AJAX for EVERYTHING! I have just inherited a project that basically consists of a single page that is populated by a ton of AJAX calls and I can tell that you just thinking about it gives me physical pain. I guess the original developer didn't like the concept of using the back/forward button in the browser as a mean of navigation.

Loris
  • 1,940
  • 3
  • 19
  • 27
2

Unless you are targeting mobile devices or other non-standard web users, you can be fairly sure that the vast majority has Javascript enabled, because most major sites (including SO) rely heavily on it.

Manu
  • 28,753
  • 28
  • 75
  • 83
2

I want my application to be as accessible as possible.

You can do things like rendering your modals and forms as a page that can operate standalone.

  • The AJAX version pulls the template into a modal/container, the standalone version checks if it's an AJAX request and renders the page including the header/footer (this can occur from the same URL if planned well)
  • The AJAX version intercepts the submit and does AJAX submission then provides an inline thank you, the non-AJAX opens a thank you page. Once again you can likely use the same pages for each of these functions if thought out correctly. Reusing templates and URL's helps avoid additional maintenance for the AJAX/non-AJAX versions.

I want my application to be enhanced by AJAX, but also fully functional without AJAX.

Thinking through the structure of your URLs and templates can go a long way towards this, if you make most of your AJAX requests pull in completely rendered templates (as opposed to just data) then you can usually use the same URL to serve both versions. You just serve only the guts of the modal/form to the AJAX request and the entire page to a regular request.

When should I not use AJAX?

  • You should not use AJAX if doing so will cause a poor experience for a significant portion of your user base (there are of course techniques that can be used to mitigate this)
  • You should not use AJAX if the development time associated with implementing it will be too significant to justify the improvements in user experience
  • You should not use AJAX for content which has significant SEO value without implementing an appropriate fallback that allows it to be indexed (Crawlers are improving constantly but it's still a good idea)

I mean, should I bother making my application usable without AJAX? Or does everyone have AJAX enabled browsers these days?

I'd say a lot of the time it's unnecessary as the vast majority of users will have AJAX enabled browsers, but there are scenarios where it's critical such as SEO optimization or when a large portion of your user base is likely to use browsers that are less likely to support Javascript as well or where they're likely to have Javascript/AJAX disabled.

A few examples of these scenarios:

  • A website for a company or government that uses an outdated browser as standard
  • A website where a large portion of the users may be disabled in a manner that may negatively impact their experience such as a website for vision or motor-skill impaired people may be negatively impacted by updating content via AJAX especially if it occurs rapidly.
    • A site accessed regularly via a less common device or browser that will cause a negative impact to a large portion of users

So what should I do?

  • Think about who is going to be using the site, how they're going to access it, and what they're going to access it with. Also try to think about not just the present but also the future.
  • Design the site in a manner that will cater to the majority of these users.
  • Think who will gain and who will loose based on my decision to use AJAX and if in doubt have a look at your analytics data to help weigh up the decision and if you lack the data it may be worth updating your tracking and obtaining a sample to aid the decision
  • Think does my decision to use AJAX cause any contradictions with core requirements for this project
  • Use AJAX to enhance content where possible as opposed to making it mandatory ie the content should work with or without JS/AJAX
  • Consider the additional development time involved with the use of AJAX (if any)
Brian
  • 2,822
  • 1
  • 16
  • 19
1

My experience is, we should use ajax after it works without it. For a couple of reasons. First, if something breaks in the ajax, and you don't have it working without it, the site simply doesn't work. For example, a product list with pagination. It should work with the links alone, then use ajax when possible. Second, for site indexing and accessibility. If it works without ajax, it's better.

And it's easier to break something (even if only for a few moments). A bad piece of code, an uncaught exception, an external library not loaded, a blocking browser extension,...

After everything works without ajax, its quite easier to add ajax. Just have the ajax catch the action, add ajax=1 and when returning the result, return only what you need if ajax=1, otherwise return everything. In the product list example, I would only return the products and pagination html, and add to the correct div. If ajax stops working, the whole page is loaded and the customer sees the second page as it loads.

Ajax adds a lot of value to UX. If done right, the user gets a great feel when using the site, and better data usage because it doesn't load the whole page everytime.

But the question being "when not to use ajax", I would say, you should always count on it to improve UX but not rely on it for the site to work (as other users also mentioned). And nowadays we need both, great code and great user experience.

sadlyblue
  • 2,992
  • 7
  • 22
  • 19
  • "if something breaks in the ajax" then we fix it, same as if something breaks in code without AJAX, we fix it – bugwheels94 Feb 09 '17 at 07:37
  • "After everything works without ajax, its quite easier to add ajax.". I don't think that's the case. With the RESTful APIs, the architecture these days are quite different. By suggesting use query parameter ajax, are you not suggesting to design both. And since you said "as other users also mentioned", they said it 8 years ago. I miss any strong point being made in this answer – bugwheels94 Feb 09 '17 at 07:44
  • Of course if something breaks, we fix it, but there is a time while the bug/error is breaking the website. If you rely only on ajax, it could be a problem and the website could not work properly. And lets not forget the client side incompatibilities. And depending on how you design the website, it could be easier to implement. The use of the query parameter is like an addon to the implementation than a new one. And my point is no never rely only on ajax. But there is no reason (unless a cheap client) to not use it. – sadlyblue Feb 09 '17 at 08:08
0

My practice is to use two main pages, let's say index.py and ajax.py. First one is responsible for generating full website, and is default target of forms. Other one generates only output specific for adequate ajax query. Logic behind both of them is the same, only the method of generating output is a bit different. In jquery I simply change action parameter when sending a request. It works both with and without ajax, although long time have I not seen someone with disabled js and ajax.

Krzysztof Bujniewicz
  • 2,407
  • 21
  • 16
0

I like the thought of coding your application without JavaScript / Ajax and then adding it in later to enhance the UI without depriving users of functionality just because they don't have JavaScript enabled. I read about this in Pro ASP.NET MVC but I think I've seen it elsewhere in reading about unobtrusive JavaScript.

Mayo
  • 10,544
  • 6
  • 45
  • 90
0

You should not make your service bloated with web 2.0 effects like accordion, modal/etc forms, image zoomers etc.
Use modern tech smarter (AJAX is one of them) and your users will be happy. Do not fear AJAX -- this is very good thing to make user expirience smooth. But don't do things because you like it - do them because your user need it ;)

NilColor
  • 3,462
  • 3
  • 30
  • 44
0

When you want to make a website that looks like a website, not a fugly imitation of a desktop app?

Igor Zevaka
  • 74,528
  • 26
  • 112
  • 128
0

You should not use AJAX or JavaScript in cases where:

  • your system needs to be accessible
  • your system needs to be search friendly

However, by using a modern JS framework with some solid "unobtrusive" practices, you can progressively enhance pages so that they remain accessible and search-friendly while offering a slick UI to users.

Toby Hede
  • 36,755
  • 28
  • 133
  • 162
0

This totally depends on the type of application or feature you're developing. If it is crucial that the application is accessible despite the absence of Javascript, then it would help to have fallback methods (i.e. alternative forms) to allow your user to use said functionality/feature. For that, it will require you to invest some of your time developing methods for collecting information not just using client-side scripts but also on the server-side.

For miscellaneous features that only serves to enhance user experience, it's mostly not worth it to develop fallback methods.

There's no reason to totally not use AJAX. AJAX helps minimize your traffic after all.

RJBaytos
  • 71
  • 1
  • 8
0

You can if you wish always use AJAX and update the history state using Push State or for more compatibility use the hash with none HTML5 compliant browsers.

with this you can have your server load a page then javascript read the document.hash and resume the state of the application base on the state of the hash.

for example i got to /index.html i click into something for example a client to open the view client you can change the hash to #/view/client/{client_id}/ then if a reload or go back using the browser the hash with change and you can use the onhashchanged event to capture it and match the sites state to the new hash then same if a favorite a certain state

Barkermn01
  • 6,781
  • 33
  • 83
0

A couple of other scenarios where one may be better off NOT using AJAX:

  • Letting someone to log into the web application. Use traditional form submit instead.

  • Searching and returning more than a few 100 rows from the database. Either break the process down or let the server side language handle it.

Dhruv Saxena
  • 1,336
  • 2
  • 12
  • 29
  • " Use traditional form submit instead." Why? What's the cons in using AJAX for submitting forms? – bugwheels94 Feb 09 '17 at 07:32
  • " Either break the process down or let the server side language handle it." Why do you think it has anything to do with AJAX? In AJAX, server side language handles databases. So what's the point – bugwheels94 Feb 09 '17 at 07:33
  • (1) It wasn't intended to be a generalised statement for all forms as such - only the ones that are meant to allow the user to sign-in into the web application. Not to say that the logins can't be done using AJAX, but since the authorisation channel would be the most critical of the user interfaces, it's rather convenient to design the application to take care of the security issues pertaining to authorisations and sign-in using the server side alone and limit AJAX to obtaining the data dynamically - once the session has been established. – Dhruv Saxena Feb 09 '17 at 07:56
  • (2) If the volume of data to be returned by the AJAX call is huge (say, a few thousand rows of 4 to 5 columns each), the server side limit on the return data could result in truncation of the information received. So, to keep the overall load on the response stream light, it might not be a bad idea to implement the lazy loaders (breaking the process down) or defining the limit on what can be fetched in a single call and creating the links such as `See More...` which will, in turn, transfer the ownership of handling this issue to the server side language. – Dhruv Saxena Feb 09 '17 at 08:02