Advertisement
rhandom

Mini AJAX Library

Apr 10th, 2012
288
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // constructor function with name of class. JScript does not really have classes and neither is this like a
  2. //PHP constructor class http://php.net/manual/en/language.oop5.decon.php
  3. function Ajax() {
  4.     // properties which are variables or constants in PHP5
  5.     this.req = null;
  6.     this.url = null;
  7.     this.method = 'GET';
  8.     this.asynch = true;
  9.     this.status = null;
  10.     this.statusText = '';
  11.     this.postData = null;
  12.     this.readyState = null;
  13.     this.responseText = null;
  14.     this.responseXML = null;
  15.     this.handleResp = null;
  16.     this.responseFormat = 'text',
  17.     // 'text', 'html', 'xml' or 'object'
  18.     this.mimeType = null;
  19.  
  20. }
  21. // End Constructor
  22. //Create XMLHttpRequest method with  XMLHttpRequest object
  23. //Ajax.prototype.init
  24. //this.init = function() {
  25.     Ajax.prototype.init = function () {
  26.     if (!this.req) {
  27.         try {
  28.             //Try to create objects for Firefox, Safari, IE7, etc.
  29.             this.req = new XMLHttpRequest();
  30.         }
  31.  
  32.         catch(e) {
  33.             try {
  34.                 //Try to create object for later versions of IE.
  35.                 this.req = new ActiveXObject('MSXML2.XMLHTTP');
  36.             }
  37.  
  38.             catch(e) {
  39.                 try {
  40.                     //Try to create for early versions of IE.
  41.                     this.req = new ActiveXObject('Microsoft.XMLHTTP');
  42.                 }
  43.  
  44.                 catch(e) {
  45.                     //Could not create XMLHttpRequest object.
  46.                     return false;
  47.                 }
  48.             }
  49.         }
  50.     }
  51.     return this.req;
  52. };
  53.  
  54.  
  55. //Sending a Request method
  56. Ajax.prototype.doReq = function() {
  57.     if (!Ajax.prototype.init()) {
  58.         alert('Could not create XMLHttpRequest object.');
  59.         return;
  60.     }
  61.     //Setting up a request
  62.     //open methods with method, url and asycn yes or no
  63.     Ajax.prototype.req.open(this.method, this.url, this.asynch);
  64.     //Make sure mimetype is OK
  65.     if (this.mimeType) {
  66.         try {
  67.             req.overrideMimeType(this.mimeType);
  68.         }
  69.         catch(e) {
  70.             //couldn't override MIME type ... IE6 or Opera?
  71.             }
  72.     }
  73.  
  74.     //var self = this;
  75.     // fix loss-of-scope in inner function
  76.     Ajax.prototype.req.onreadystatechange = function() {
  77.         var resp = null;
  78.         if (Ajax.prototype.req.readyState == 4) {
  79.             //do stuff to handle response
  80.             switch (this.reponseFormat) {
  81.             case 'text':
  82.                 resp = this.req.responseText;
  83.                 break;
  84.             case 'xml':
  85.                 resp = this.req.responseXML;
  86.                 break;
  87.             case 'object':
  88.                 resp = req;
  89.                 break;
  90.             }
  91.             if (Ajax.prototype.status >= 200 && Ajax.prototype.req.status <= 299) {
  92.                 this.handleResp(resp);
  93.             }
  94.             else {
  95.                 Ajax.prototype.handleErr(resp);
  96.             }
  97.         }
  98.  
  99.     };
  100.     Ajax.prototype.req.send(this.postData);
  101. };
  102.  
  103. Ajax.prototype.handleErr = function() {
  104.     var errorWin;
  105.     try {
  106.         errorWin = window.open('', 'errorWin');
  107.         errorWin.document.body.innerHTML = this.responseText;
  108.     }
  109.     catch(e) {
  110.         alert('An error occured, but this error message cannot be '
  111.         + 'displayed. This is probably because of your browser\'s '
  112.         + 'pop-up blocker. \n'
  113.         + 'Please  allow pop-ups from this website if you want to '
  114.         + 'see the full error messages. \n'
  115.         + '\n'
  116.         + 'Status Code: ' + this.req.status + '\n'
  117.         + 'Status description: ' + this.req.statusText);
  118.     }
  119. };
  120.  
  121. Ajax.prototype.abort = function() {
  122.     if (this.req) {
  123.         this.req.onreadystatechange = function() {};
  124.         this.req.abort();
  125.         this.req = null;
  126.     }
  127. };
  128.  
  129. Ajax.prototype.doGet = function (url, hand, format) {
  130.     this.url = url;
  131.     this.handleResp = hand;
  132.     this.responseFormat = format || 'text';
  133.     this.doReq();
  134. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement