Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how AJAX application should behave when Javascript is disabled - common practice?

I’m in the process of developing pretty basic web application, that is mostly so I could learn jQuery/ajax/php on the way (and have some fun). I want to make it as accessible to users as possible so it should work with Javascript disabled, validate to AAA and all that. With JS disabled would be of course without all the bells and whistles, but nevertheless should do the job.

I would like to make good use of Ajax, but I don’t fully understand how should I cope when JS is off.

So let’s say JS is on, user submits the form, clicks submit button and thru ajax, data is submitted to register.php (register.php is specified in forms action attribute). register.php returns data and jQuery displays appropriate message. All without reloading the page.

Now, if JS is disabled, submitting form to register.php won’t do much good.

The way I understand it, solution would be to create one php script for JS enabled, other for JS disabled. So by default form would have action attribute with nonjs_register.php, and if JS would be enabled, it would force the form to be submitted to js_register.php rather than default nonjs_register.php. I can imagine that would be quite tedious to create two scripts pages for each user interaction with the application but that’s the only way I can think of at the moment.

I hope all that makes sense but please let me know if my explanation is not quite clear. Now if anyone could explain to me what is the common practice to deal with that kind of problem that would be great.

like image 229
spirytus Avatar asked Dec 09 '09 23:12

spirytus


People also ask

Will AJAX work if JavaScript is disabled?

Ajax call works when javascript is enabled. You can handle it by server-side scripting, when javascript is disabled, you must do works by post/get requests, so you have to recode your web application.

Do you need JavaScript for AJAX?

Ajax isn't a part of JavaScript. They're just used together in the web environment. (Side note: Although Ajax stands for "Asynchronous JavaScript and XML", the XML part of that is optional; you can do "Ajax" without using XML and in fact, many if not most people do.

What should you not use AJAX on?

Use caution and test a small area first on delicate surfaces such as fiberglass, imitation marble, plastics, and enameled appliances. Use plenty of water, rub gently and rinse well. Do not use on silver, fabrics, painted surfaces or plexiglass.

Why JavaScript and AJAX have been ignored for web application on the mobile phones?

JavaScript and Ajax have been ignored because using an Ajax - based web application on your phone can drain your battery at a rate of four to five times your normal power consumption . JavaScript consumes more processor power and therefore more battery life .


2 Answers

Take a look at the Hijax technique, it's a way of using AJAX as a progressive enhancement.

like image 130
Chris Fulstow Avatar answered Sep 22 '22 01:09

Chris Fulstow


The way I would deal with this sort of thing is to use an event with javascript that cancels the default action of the form. The default action of the form is to submit to a different url:

html

<form id="AjaxForm" action="/nonJS_register.php" method="POST">
   <!-- form input elements -->
</form>

js

document.getElementById("AjaxForm").onsubmit = function ()
{
     //- do ajax posting...
     ajaxPostForm();

     //- Cancel the default action of the form
     event.preventDefault();
     return false;
}

The ajax function could submit to nonJS_register or you could even just add a parameter that tells the script to return the data in a different format and not encapsulated by HTML.

like image 38
Andy E Avatar answered Sep 21 '22 01:09

Andy E