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

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
Compressed Source File

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

Demo

jxs Demonstration.

Code

Source - jxs.js File
Compressed Source File

Comments

ben at 22 Mar, 2007 05:47
I'm using jx excess - V2.01.A.
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).
Reply to this.
Binny V A at 23 Mar, 2007 05:46
Thanks for alerting me - I will fix it as soon as possible.
Reply to this.
Binny V A at 23 Mar, 2007 01:01
Ok - its fixed now. Thanks for reporting this.
Reply to this.
wyo at 05 Apr, 2007 12:48
How about publishing the full sample code (server and client side) for download?
Reply to this.
Binny V A at 05 Apr, 2007 08:57
If you give me your email address, I will send you the server side code. I don't see any point in publishing it here. If you want the code, just send me a mail. My Address is binnyva, gmail.
Reply to this.
Thiago Ricieri at 16 Apr, 2007 06:15
Hi,

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.
Reply to this.
Binny V A at 16 Apr, 2007 10:21
Hate to break this to you - but I have already done that - in jxs.
Reply to this.
Anonymous at 20 Apr, 2007 11:47
Can't find the license...
Reply to this.
Binny V A at 20 Apr, 2007 12:20
BSD License - like all other scripts in this site.
Reply to this.
Anonymous at 02 Jun, 2007 09:08
this maybe is using for ajax coders.
I just found another Ajax loading generator on www.webscriptlab.com, lets you generate loading icons on demand for your applications. Pretty cool stuff! ...
Reply to this.
Anonymous at 18 Jul, 2007 11:01
I would like to use the "data" variable as a global variable. How would you do that? Thanks!
Reply to this.
Binny V A at 19 Jul, 2007 01:33
Do something like this

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.
Reply to this.
gymnasium at 12 Aug, 2007 06:41
nice site...and nice tutorial
Reply to this.
Saqib Razzaq at 05 Sep, 2007 12:36
its a nice script. I want to use it with ASP.Net and SQL Server DB values based on the search text entered in the textbox.
Reply to this.
Binny V A at 05 Sep, 2007 08:00
This is a client side technology - it does not matter what you are using on the server.
Reply to this.
Anonymous at 30 Oct, 2007 08:26
It would be nice if there was some proper documentation, other than the demo app - which frankly I can't make head or tail of.
Reply to this.
Alan Burlison at 31 Oct, 2007 10:20
The current compressed version of jxs.js generates some Javascript warnings, diffs below:

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"){
Reply to this.
Franz Fortuny at 12 Nov, 2007 10:57

Using this function:


function ValidaUser()
{
jx.load("valuser?user="+document.forms[0].user.value,
function(data){document.getElementById('user').innerHTML=data;},
"text","GET");
}

It returns the proper values from the server, BUT the Error Console reports this:


Error: error de sintaxis
Archivo Fuente: localhost/cgi-bin/glaw.exe/valuser?user=frafor&uid=1194892808062
Línea: 1, Columna: 1
Código Fuente:
No válido^


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:


Error: error de sintaxis
Archivo Fuente: localhost/cgi-bin/glaw.exe/valuser?user=kjkdj&uid=1194892730953
Línea: 1, Columna: 1
Código Fuente:
Válido^

It is as if your function were collecting the response from my application as part of a Javascript code.


Good work!

Reply to this.
Yaroslav at 22 Nov, 2007 05:26
jxs still has "if (XMLHttpRequest)" code instead of "if (window.XMLHttpRequest)".
Please fix it and it will be just a perfect! I like it! Thank you!
Reply to this.
Anonymous at 29 Nov, 2007 08:35
Hi,

I do have a problem with multiple ajax calls - e.g. :
var litRepURL = "Locus2Publications?locus=" + agiCode + "&queryID=" + squeryID;
jx.load(litRepURL, function(data){
showAggregator(data, "litrep");
}, "json","get");

var taigaURL = "Locus2Images?locus=" + agiCode + "&queryID=" + squeryID;
jx.load(taigaURL, function(data){
showAggregator(data, "taiga");
}, "json","get");

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
Reply to this.
germán at 22 May, 2008 03:28
Hello, I have the same problem. I modified the jx.js file to avoid references to "this". Now it's works OK at least on IE6 and FF. I don't know what consecuences will this have....

Hear is the new code:

//V3.00.A - www.openjs.com/scripts/jx/
// modified to enable (or at least try to enable) concurrent use
jx = {
//http:false, //HTTP Object
//format:'text',
//callback:function(data){},
//error:false,
//Create a xmlHttpRequest object - this is the constructor.
getHTTPObject : function() {
var http = false;
//Use IE's ActiveX items to load the file.
if(typeof ActiveXObject != 'undefined') {
try {http = new ActiveXObject("Msxml2.XMLHTTP");}
catch (e) {
try {http = new ActiveXObject("Microsoft.XMLHTTP");}
catch (E) {http = false;}
}
//If ActiveX is not available, use the XMLHttpRequest of Firefox/Mozilla etc. to load the document.
} else if (XMLHttpRequest) {
try {http = new XMLHttpRequest();}
catch (e) {http = false;}
}
return http;
},
// This function is called from the user's script.
//Arguments -
// url - The url of the serverside script that is to be called. Append all the arguments to
// this url - eg. 'get_data.php?id=5&car=benz'
// callback - Function that must be called once the data is ready.
// format - The return type for this function. Could be 'xml','json' or 'text'. If it is json,
// the string will be 'eval'ed before returning it. Default:'text'
load : function (url,callback,format) {

var http = this.init(); //The XMLHttpRequest object is created at every call - to defeat Cache problem in IE
if(!http||!url) return;
if (http.overrideMimeType) http.overrideMimeType('text/xml');

if(!format) var format = "text";//Default return type is 'text'
format = format.toLowerCase();

if (http.overrideMimeType) http.overrideMimeType('text/xml');

//Kill the Cache problem in IE.
var now = "uid=" + new Date().getTime();
url += (url.indexOf("?")+1) ? "&" : "?";
url += now;

http.open("GET", url, true);

http.onreadystatechange = function () {//Call a function when the state changes.
if (http.readyState == 4) {//Ready State will be 4 when the document is loaded.
if(http.status == 200) {
var result = "";
if(http.responseText) result = http.responseText;
//If the return is in JSON format, eval the result before returning it.
if(format.charAt(0) == "j") {
//\n's in JSON string, when evaluated will create errors in IE
result = result.replace(/[\n\r]/g,"");
result = eval('('+result+')');
}

//Give the data to the callback function.
if(callback) callback(result);
} else { //An error occured
if(error) error()
}
}
}
http.send(null);
},
init : function() {return this.getHTTPObject();}
}
Reply to this.
FIG-GHD742 at 15 Apr, 2008 07:31
Hello.

I is rely happy for you script,
Is is vary esye to user.
Vary god, i tink ajax normal is vary hard.


/ FIG
Reply to this.
Anonymous at 19 May, 2008 05:04
I ran into the same problem with jx callbacks being overwritten by multiple requests. So I now clone the jx object and call clone(jx).load instead of jx.load

function clone(myObj)
{
if(typeof(myObj) != 'object') return myObj;
if(myObj == null) return myObj;
var myNewObj = new Object();
for(var i in myObj) myNewObj[i] = clone(myObj[i]);
return myNewObj;
}
Reply to this.
Anonymous at 28 May, 2008 10:07
Interesting, this function may be supposed to help newbie (like me), but after few hours of trying to figure out how to use it, I still don't know how to access data that it fetched from php, but I know how 90% of it works.
I think you need more examples.
Reply to this.
Europe72 at 31 Jul, 2008 07:58
Thanks for putting this simple, small AJAX lib together. I just used it in a BSD-licensed project called Spinetail (http://code.google.com/p/spinetail/). JX was exactly what I was looking for, a no frills lib with a small footprint; easy to use and understand.
Reply to this.
Anonymous at 09 Oct, 2008 06:36
I just wanted to say thanks for this amazing library! It's so small which is great for mobile devices which I happen to work on. And it's only 2.9KB in size! Plus it's so easy to use, only 1 line of code and that's it. This site could do with more example tutorials on how to use it though.
Thanks again.
Reply to this.
tamat at 11 Oct, 2008 05:58
wow, amazingly simply. Great work.

btw, I have the same problem as the other users, I can't have multiple request at the same time but I hope it will be fixed in next versions.

thanks!
Reply to this.
[e]Bu$ter at 12 Oct, 2008 01:11
In jx error-handler calling like this:
if(ths.error) ths.error()
It's bad - no specified parameters.

In jxs error-handler calling like this:
if(ths.error) ths.error(http.status); (good - I can see why this error occurred)
But, if use method 'bind', for calling jxs, error-handler again calling without arguments:
if(opt.onError) this.error = opt.onError;

May be give arguments in all calling?

P.S. Thanx for jx.
Sorry for my inglish :)
Reply to this.
[e]Bu$ter at 12 Oct, 2008 02:20
And yet:
The line if (this.http.overrideMimeType) this.http.overrideMimeType('text/xml');
in lite version meets twice. Mistake?
Reply to this.
Binny V A at 13 Oct, 2008 03:56
Thanks for the tips - I'll incorporate them into the code as soon as possible.
Reply to this.
Binny V A at 13 Oct, 2008 09:53
All right - I have made the changes. Once again, thanks for your feedback.
Reply to this.
albertux7 at 15 Oct, 2008 12:35
Good and simple script, I want to use jx.js on one personal project good job Binny V A
Reply to this.
Anonymous at 21 Nov, 2008 04:46
Hi would really like some help. I am new to all of this. I tried to use your script with out much success. Even in the demo page, if i set the url to "www.openjs.com/scripts/jx/data.php..." it does not work. So how would i post to a script on a nother website to mine? Any help would be very very appreciated.
Thankyou.
Reply to this.
Binny V A at 21 Nov, 2008 09:27
You can only call pages within your domain. Its a security feature in Ajax.
Reply to this.
Nico at 08 Dec, 2008 09:41
Hey! I like your js-Ajax-Library! It's better than 10 KB of code which finally has the same function. But I've got a problem with declaring global vars... If you do it on the normal way like that:

jx.load('demo.php?dummy=text&lorem=ipsum', function(data){
returned_data = data;
});

...returned_data is undefined in further code. I call jx.load always in a function, like this:

function name() {
jx.load('demo.php?dummy=text&lorem=ipsum', function(data){
returned_data = data;
});
}

Does it have to do something with that?

Thank you very much for your help...
Reply to this.
Binny V A at 13 Dec, 2008 08:34
There is very little you can do about that. The function is called asynchronously(outside the flow of the script) - so you will not get the value of the global variable until after the function is called. You may be able to fix it by using synchronous mode - but thats not recommendeded.
Reply to this.
Anonymous at 17 Dec, 2008 01:48
Hi, I really like your jxs code! It's great - and will be perfect for a new project i'm working on. I would like to be able to call this more then once at the same time -- So the modified version by germán to allow concurrent use looks good to me. But i would like to hear what the original author thinks of the changed code.

Also the comment by Yaroslav about jxs still having "if (XMLHttpRequest)" code instead of "if (window.XMLHttpRequest)". -- as my javascript skills are lacking i do not know if this is a required change (i could change it, but i dont know if it would be for the best)

Thanks for your help!

Cheers.
Reply to this.
^Chaos13^ at 17 Dec, 2008 09:52
Cool script :P Saves a lot of time
+1
Reply to this.
Anonymous at 18 Dec, 2008 07:00
Binny V A -- Can you fix jxs to allow for concurrent use, and to fix the window.XMLHttpRequest thanks!
Reply to this.
Anonymous at 02 Mar, 2009 07:19
in your code there is line like this:if(error) error(http.status), but when http error occurs, it will cause javascript error, because error is undefined.
Reply to this.
Nick at 08 Mar, 2009 06:29
TO ALL:

If you want "data" to be global, then you can do something like this:

<script>
var my_global_data_variable;
js.load('/whatever/?blah=bleh', function(data){
my_global_data_variable = data;
});
</script>
Reply to this.
Anonymous at 11 Mar, 2009 10:11
Is there a simple way to make sequential requests as follows:

jx.load(req_1);
// Wait and process the req_1 result here
jz.load(req_2);
// Wait and process the req_1 result here
...

Of course, I can place jz.load(req_2); into the callback fn for req_1 etc., but it makes the code too unclean for reading.
Reply to this.
Sag-e-Attar Junaid Atari at 14 Mar, 2009 02:48
Where i can get the Documentation of JX?
Reply to this.
Sag-e-Attar Junaid Atari at 14 Mar, 2009 03:02
JXE loading indicator?

How to do it?

Please :(
Reply to this.
Junaid Atari at 16 Mar, 2009 07:33
Here i got an error:

'error' undefined...

What should i dow?
Reply to this.
Tayfun at 24 Apr, 2009 03:55
@Juanid Atari you should create a function whose name is "error" to handle ajax errors. it accepts http status of the related request.
ie :
function error(status){
//do the stuff
}
@Binny V A, i think registering a custom error handler will be good, ie we can pass error handler as a function parameter.
Thnx for this simple and great library.
Reply to this.
Comment

Please dont enter you comments in this form - this is a fake form to confuse spamming bots. The next form is the real one.




Comment




Comment Formating : HTML tags a, strong, em, b, i, code, pre, p and br allowed. Other tags will be shown as code(< will become &lt;). Urls, Line breaks will be auto-formated.