fork download
  1. /*
  2.  * Simple AJAX
  3.  * Written By: Tyler O'Brien (tyler@indie-dev.net)
  4.  * File Version: 2012.04.25.1
  5.  * */
  6.  
  7. /*
  8.  * Create a fallback for IE6.
  9.  * IE5 and earlier might not work.
  10.  * */
  11. if (typeof XMLHttpRequest == 'undefined') {
  12. var XMLHttpRequest = function() {
  13. return new ActiveXObject('Microsoft.XMLHTTP');
  14. }
  15. XMLHttpRequest.prototype.isActiveX = true;
  16. }
  17.  
  18. XMLHttpRequest.prototype.send_s = function(method, queryString) {
  19. if (method == 'POST') this.send(queryString);
  20. else if (this.isActiveX) this.send();
  21. else this.send(null);
  22. }
  23.  
  24. /*
  25.  * AjaxHandler
  26.  * This object contains boilerplate functions that help organise the code of Simple AJAX.
  27.  * These probably won't need to be used outside of the AjaxRequest constructor function.
  28.  * Note:
  29.   * These functions are also where the default values are assigned.
  30.  * */
  31. var AjaxHandler = {
  32. /*
  33. * CallIfExists() returns nothing
  34. * Input: Function, Mixed, Mixed
  35. * Takes a callback, and up to two optional parameters.
  36. * Depending on the number of parameters given will make one of three different calls.
  37. * */
  38. CallIfExists: function(callback, param1, param2) {
  39. if (callback) {
  40. if (param2) callback(param1, param2);
  41. else if (param1) callback(param1);
  42. else callback();
  43. }
  44. },
  45.  
  46. /*
  47. * GetCarset() returns String
  48. * Input: String
  49. * Takes a passed object, assumed String.
  50. * If NULL or UNDEFINED is passed then a default value is returned.
  51. * Otherwise the passed value is returned back.
  52. * */
  53. GetCharset: function(charset) {
  54. return (charset) ? charset : 'utf-8';
  55. },
  56.  
  57. /*
  58. * GetContentType() returns String
  59. * Input: String
  60. * See GetCharset().
  61. * */
  62. GetContentType: function(contentType) {
  63. return (contentType) ? contentType : 'application/x-www-form-urlencoded';
  64. },
  65.  
  66. /*
  67. * GetMethod() returns String
  68. * Input: String
  69. * See GetCharset().
  70. * */
  71. GetMethod: function(method) {
  72. return (method) ? method : 'GET';
  73. },
  74.  
  75. /*
  76. * GetQueryString() returns String
  77. * Input: Object
  78. * Takes an object of (key=>value) pairs and converts them to a query string.
  79. * For example:
  80. * The object {a:1,b:2,c:3} would become "a=1&b=2&c=3".
  81. * Note that the question mark is not appended to the beginning.
  82. * */
  83. GetQueryString: function(data) {
  84. var output = '';
  85.  
  86. if (data) {
  87. var output = '';
  88.  
  89. for (var key in data) {
  90. output = (output + key +'='+ data[key] + '&');
  91. }
  92.  
  93. // Trim off the extra ampersand.
  94. output = output.substring(0, output.length-1);
  95. }
  96.  
  97. return output;
  98. },
  99.  
  100. /*
  101. * GetUrl() returns String
  102. * Input: String
  103. * See GetCharset().
  104. * */
  105. GetUrl: function(url) {
  106. return (url) ? url : '/';
  107. },
  108.  
  109. /*
  110. * GetUrlToOpen() returns String
  111. * Input: String, String, String
  112. * Returns the value that will be passed to the XMLHttpRequest.open() function.
  113. * If the method is a GET it will be the entire URL, as well as the query string.
  114. * If the method is a POST then it will only be the query string.
  115. * */
  116. GetUrlToOpen: function(url, queryString, method) {
  117. return (method == 'POST') ? url : (url + '?' + queryString);
  118. }
  119. };
  120.  
  121. /*
  122.  * AjaxRequest
  123.  * The base class which holds all of the objects used to make AJAX requests.
  124.  * Is implemented in the Ajax() function.
  125.  * */
  126. function AjaxRequest(args) {
  127. this.charset = AjaxHandler.GetCharset(args['charset']);
  128. this.contentType = AjaxHandler.GetContentType(args['contentType']);
  129. this.http = new XMLHttpRequest();
  130. this.method = AjaxHandler.GetMethod(args['method']);
  131. this.onConnect = args['onConnect'];
  132. this.onError = args['onError'];
  133. this.onNotFound = args['onNotFound'];
  134. this.onProcess = args['onProcess'];
  135. this.onRequest = args['onRequest'];
  136. this.onSuccess = args['onSuccess'];
  137. this.onTimeout = args['onTimeout'];
  138. this.queryString = AjaxHandler.GetQueryString(args['data']);
  139. this.timeBegin = 0;
  140. this.timeDifference = 0;
  141. this.timeEnd = 0;
  142. this.timeout = null;
  143. this.url = AjaxHandler.GetUrl(args['url']);
  144. this.urlToOpen = AjaxHandler.GetUrlToOpen(this.url, this.queryString, this.method);
  145. }
  146.  
  147. /*
  148.  * Ajax() returns nothing
  149.  * Input: Object
  150.  * The main function for Simple AJAX.
  151.  * Every time this function is called it will create a new AjaxRequest object.
  152.  *
  153.  * Example usage:
  154.   * Ajax({
  155. * method: 'GET',
  156. * url: '/path/to/file.html',
  157. * onSuccess: function(response){
  158. * console.log('Received: ' + response);
  159. *});
  160.  * */
  161. function Ajax(args) {
  162. var ajaxRequest = new AjaxRequest(args);
  163.  
  164. ajaxRequest.http.open(ajaxRequest.method, ajaxRequest.urlToOpen, true);
  165. ajaxRequest.http.setRequestHeader('Content-type', ajaxRequest.contentType+'; charset='+ajaxRequest.charset);
  166.  
  167. if (ajaxRequest.method == 'POST') {
  168. ajaxRequest.http.setRequestHeader('Content-length', ajaxRequest.queryString.length);
  169. ajaxRequest.http.setRequestHeader('Connection', 'close');
  170. }
  171.  
  172. ajaxRequest.http.onreadystatechange = function(){
  173. switch (ajaxRequest.http.readyState) {
  174. case 1:
  175. ajaxRequest.timeBegin = new Date().getTime();
  176. AjaxHandler.CallIfExists(ajaxRequest.onConnect);
  177. break;
  178. case 2:
  179. AjaxHandler.CallIfExists(ajaxRequest.onRequest);
  180. break;
  181. case 3:
  182. AjaxHandler.CallIfExists(ajaxRequest.onProcess);
  183. break;
  184. case 4:
  185. if (ajaxRequest.timeout) {
  186. clearTimeout(ajaxRequest.timeout);
  187. }
  188.  
  189. ajaxRequest.timeEnd = new Date().getTime();
  190. ajaxRequest.timeDifference = (ajaxRequest.timeEnd - ajaxRequest.timeBegin);
  191.  
  192. switch (ajaxRequest.http.status) {
  193. case 200:
  194. AjaxHandler.CallIfExists(ajaxRequest.onSuccess, ajaxRequest.http.responseText, ajaxRequest.timeDifference);
  195. break;
  196. case 404:
  197. AjaxHandler.CallIfExists(ajaxRequest.onNotFound, ajaxRequest.http.responseText, ajaxRequest.timeDifference);
  198. default:
  199. AjaxHandler.CallIfExists(ajaxRequest.onError, ajaxRequest.http.status, ajaxRequest.timeDifference);
  200. break;
  201. }
  202. break;
  203. }
  204. };
  205.  
  206. /*
  207. * Add a new timeout if one is given.
  208. * The time is assumed to be milliseconds.
  209. * */
  210. if (args['timeout']) {
  211. ajaxRequest.timeout = setTimeout(function() {
  212. ajaxRequest.http.abort();
  213. AjaxHandler.CallIfExists(ajaxRequest.onTimeout);
  214. }, args['timeout']);
  215. }
  216.  
  217. ajaxRequest.http.send_s(ajaxRequest.method, ajaxRequest.queryString);
  218. }
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty