Site Moved

This site has been moved to a new location - Bin-Blog. All new post will appear at the new location.

Bin-Blog

Updated version of jx Ajax Library

The smallest Ajax library - jx has been very useful for me - so much that it found its way to the common.js file of the Bin-Co site. So I thought that I will release a slightly updated version.


// jx Library - http://binnyva.blogspot.com/2006/02/jx-ajax-library-in-object-notation.html (Version 1.01.A)
var jx = {
 http : false,
 getHTTPObject : function() {
  var xmlhttp = false;
  if(typeof ActiveXObject != 'undefined') {
   try {xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");}
   catch (e) {
    try {xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");}
    catch (E) {xmlhttp = false;}
   }
  } else if (XMLHttpRequest) {
   xmlhttp = new XMLHttpRequest();
  }
  return xmlhttp;
 },
 getData : function(url,callback,type) {
  if(!this.http) {
   this.init();
   if(!this.http) return;
  }
  if(!type) var type = "text";
  this.http.open("GET", url, true);
  this.http.onreadystatechange = function () {
   if (jx.http.readyState == 4) {
    var result = "";
    if(jx.http.responseText) result = jx.http.responseText;
    type = type.toLowerCase();
    if(type == "json" || type == "j") result = eval(result);
    if(callback) callback(result);
   }
  }
  this.http.send(null);
 },

 init : function() {
  this.http = this.getHTTPObject();
 }
}


//////////////// Init ///////////
//Call it like this.
function init() {
 jx.getData("data.php?id=3&whatever=something",function (data) {
  alert(data);
 },'t');
}
window.onload=init;

Changes

Very little changes to speak of, but here goes.

  • Removed the comments to make the code smaller - if you need to see how the code works, see the original post on the jx Ajax Library.
  • Don't have to call the jx.init() function - the script does it automatically.
  • Made the code smaller.
  • Some more small changes.

0 Comments: