jx - JavaScript Ajax Library
jx is a small toolkit for providing AJAX support in JavaScript. It has two different version - jx and jxs.
jx - Lite Vesion - V3.00.A
This provides the most basic Ajax support. It is striped down to minimise file size - so it will not support many advanced features. You can pass data to the server side script using the GET method only. The supported return types are plain text and JSON. It does NOT support XML return.
See a line by line explanation of what the code does...
Demo
jx Demonstration.Features
- Supports GET method
- Supports JSON.
- Small Size (1.2 KB compressed / 2.6 KB with all the comments)
- Very easy to use.
Samples
jx.load('demo.php?dummy=text&lorem=ipsum',function(data){
alert(data); // Do what you want with the 'data' variable.
});
For more samples, see the jx Demonstration page. Code is provided for more advanced uses of jx like using JSON returns.
Code
Source - jx.js File - 2.6 KB
Compressed Source File - 1.2 KB
Take a look at the explanation page to see what happens in the code.
jxs - jx excess - V2.01.A
This is a slightly larger version of jx - hence the name j excess. It supports many features that the lite version don't support. The file size is slightly bigger - but its worth it.
Features
- Supports both POST and GET methods
- Supports XML return.
- Supports customizable loading indicator display
- Automatically update a given element with returned text.
- Customizable
onReadyStateChangehandler - And more...
Demo
jxs Demonstration.Code
Source - jxs.js File - 5.6 KB
Compressed Source File - 2.9 KB

Comments
The code in jxs.js where it says "if (XMLHttpRequest)" will give this error in IE6:
'XMLHttpRequest' is undefined
I had to change it to "if (window.XMLHttpRequest)" to get it to work in IE6 (and it still works in IE7, Opera 9.10, Firefox 1.5.0.10).
I'm using the jx (it helps me a lot ehehhe) and I discovered that it will always send the variables using the GET method. I needed to send using POST method, so I've changed the source to support the POST method too.
I just found another Ajax loading generator on www.webscriptlab.com, lets you generate loading icons on demand for your applications. Pretty cool stuff! ...
jx.load('demo.php?dummy=text&lorem=ipsum',function(data){
returned_data = data;
alert(data); // Do what you want with the 'data' variable.
});
Now the 'returned_data' is a global variable with the contents of data.
Index: jxs.js
*** /home1/homedir/alanbur/tonic/auth/trunk/AuthWebapp/web/javascript/jxs.js Base (BASE)
--- /home1/homedir/alanbur/tonic/auth/trunk/AuthWebapp/web/javascript/jxs.js Locally Modified (Based On LOCAL)
***************
*** 1,10 ****
! jx={http:false,format:'text',callback:function(data){},handler:false,error:false,opt:new Object(),
getHTTPObject:function(){var http=false;if(typeof ActiveXObject !='undefined'){try{http=new ActiveXObject("Msxml2.XMLHTTP");}catch(e){
try{http=new ActiveXObject("Microsoft.XMLHTTP");}catch(E){http=false;}}
}else if(XMLHttpRequest){try{http=new XMLHttpRequest();}catch(e){http=false;}}return http;},
load:function(url,callback,format,method){this.init();if(!this.http||!url)return;
if(this.http.overrideMimeType)this.http.overrideMimeType('text/xml');this.callback=callback;
! if(!method)var method="GET";if(!format)var format="text";this.format=format.toLowerCase();
method=method.toUpperCase();var ths=this;var now="uid="+new Date().getTime();url+=(url.indexOf("?")+1)?"&":"?";
url+=now;var parameters=null;if(method=="POST"){var parts=url.split("\?");url=parts[0];parameters=parts[1];}
this.http.open(method,url,true);if(method=="POST"){
--- 1,10 ----
! var jx={http:false,format:'text',callback:function(data){},handler:false,error:false,opt:new Object(),
getHTTPObject:function(){var http=false;if(typeof ActiveXObject !='undefined'){try{http=new ActiveXObject("Msxml2.XMLHTTP");}catch(e){
try{http=new ActiveXObject("Microsoft.XMLHTTP");}catch(E){http=false;}}
}else if(XMLHttpRequest){try{http=new XMLHttpRequest();}catch(e){http=false;}}return http;},
load:function(url,callback,format,method){this.init();if(!this.http||!url)return;
if(this.http.overrideMimeType)this.http.overrideMimeType('text/xml');this.callback=callback;
! if(!method)method="GET";if(!format)format="text";this.format=format.toLowerCase();
method=method.toUpperCase();var ths=this;var now="uid="+new Date().getTime();url+=(url.indexOf("?")+1)?"&":"?";
url+=now;var parameters=null;if(method=="POST"){var parts=url.split("\?");url=parts[0];parameters=parts[1];}
this.http.open(method,url,true);if(method=="POST"){
Using this function:
It returns the proper values from the server, BUT the Error Console reports this:
As you can see, Firefox puts the error in the language of the user (Spanish, in this case).
No matter what textual response I get from the server "Válido" or "No válido", I get in the Error Console the indicated error. Here is the other case:
It is as if your function were collecting the response from my application as part of a Javascript code.
Good work!
Please fix it and it will be just a perfect! I like it! Thank you!
I do have a problem with multiple ajax calls - e.g. :
both servlets are called but only the second callback function is then called. Apparently it the second overwrites the first callback function ?
Can you explain how to solve that ?
Thanks
I is rely happy for you script,
Is is vary esye to user.
Vary god, i tink ajax normal is vary hard.
/ FIG
a, strong, em, b, i, code, pre, pandbrallowed. Other tags will be shown as code(< will become <). Urls, Line breaks will be auto-formated.