ajax.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. function AjaxErrorBadStatusCode(message) { this.name = 'AjaxErrorBadStatusCode'; this.message = message; };
  2. AjaxErrorBadStatusCode.prototype = new Error();
  3. var ajax = {};
  4. ajax.create = function() {
  5. if(typeof XMLHttpRequest !== 'undefined') {
  6. return new XMLHttpRequest();
  7. };
  8. var versions = [
  9. "MSXML2.XmlHttp.6.0",
  10. "MSXML2.XmlHttp.5.0",
  11. "MSXML2.XmlHttp.4.0",
  12. "MSXML2.XmlHttp.3.0",
  13. "MSXML2.XmlHttp.2.0",
  14. "Microsoft.XmlHttp"
  15. ];
  16. var xhr;
  17. for(var i = 0; i < versions.length; i++) {
  18. try {
  19. xhr = new ActiveXObject(versions[i]);
  20. break;
  21. } catch(e) {
  22. // Silent
  23. };
  24. };
  25. return xhr;
  26. };
  27. ajax.send = function(url, callback, method, data, async) {
  28. if(async === undefined) {
  29. async = true;
  30. };
  31. var a = ajax.create();
  32. a.open(method, url, async);
  33. a.onreadystatechange = function() {
  34. // a.readyState:
  35. // 0 - UNSENT
  36. // 1 - OPENED
  37. // 2 - HEADERS_RECEIVED
  38. // 3 - LOADING
  39. // 4 - DONE
  40. callback(method, data, a.readyState, a.status, a.responseText);
  41. };
  42. if(method == 'POST') {
  43. a.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
  44. };
  45. a.send(data);
  46. };
  47. ajax.get = function(url, data, callback, async) {
  48. var query = [];
  49. for(var key in data) {
  50. query.push(encodeURIComponent(key) + '=' + encodeURIComponent(data[key]));
  51. };
  52. ajax.send(url + (query.length ? '?' + query.join('&') : ''), callback, 'GET', null, async);
  53. };
  54. ajax.getJSON = function(url, data, callbackSuccess, callbackError, async) {
  55. ajax.get(url, data, function(method, data, readyState, status, responseText) {
  56. if(readyState == 4) {
  57. if(status == 200) {
  58. try {
  59. var r = JSON.parse(responseText);
  60. if(callbackSuccess) {
  61. callbackSuccess(method, data, readyState, status, r);
  62. };
  63. } catch(e) {
  64. if(callbackError) {
  65. callbackError(method, data, readyState, status, e);
  66. };
  67. };
  68. } else {
  69. if(callbackError) {
  70. var e = new AjaxErrorBadStatusCode('Bad status code '+status);
  71. callbackError(method, data, readyState, status, e);
  72. };
  73. };
  74. };
  75. }, async);
  76. };
  77. ajax.post = function(url, data, callback, async) {
  78. var query = [];
  79. for (var key in data) {
  80. query.push(encodeURIComponent(key) + '=' + encodeURIComponent(data[key]));
  81. };
  82. ajax.send(url, callback, 'POST', query.join('&'), async);
  83. };
  84. ajax.loadTag = function(tag, url, func, field) {
  85. if((typeof window[func] === 'function') || field != null) {
  86. if(!!!tag.className.match(new RegExp('(\\s|^)loading(\\s|$)'))) {
  87. tag.className += " loading";
  88. ajax.getJSON(url, {}, function(method, data, readyState, status, responseData) {
  89. try {
  90. if(typeof window[func] === 'function') {
  91. var html = window[func](tag, responseData);
  92. tag.innerHTML = html;
  93. } else if(field != null) {
  94. tag.innerHTML = responseData[field];
  95. };
  96. } catch(e) {
  97. console.log('ajax.loadTag', 'e', e);
  98. };
  99. tag.className = tag.className.replace(new RegExp('(\\s|^)loading(\\s|$)'), ' ').trim();
  100. }, function(method, data, readyState, status, responseData) {
  101. tag.className = tag.className.replace(new RegExp('(\\s|^)loading(\\s|$)'), ' ').trim();
  102. });
  103. };
  104. };
  105. };
  106. ajax.processTag = function(tag) {
  107. var get = tag.getAttribute('data-ajax-get');
  108. var func = tag.getAttribute('data-ajax-func');
  109. var field = tag.getAttribute('data-ajax-field');
  110. var delay = tag.getAttribute('data-ajax-delay');
  111. if((get && get != null) && ((func && func != null) || (field && field != null))) {
  112. if(delay == null) {
  113. ajax.loadTag(tag, get, func, field);
  114. } else {
  115. setTimeout(function() {
  116. ajax.loadTag(tag, get, func, field);
  117. }, delay);
  118. };
  119. };
  120. };
  121. ajax.processTags = function() {
  122. var tags = document.querySelectorAll('[data-ajax-get]');
  123. for(var key in tags) if(tags.hasOwnProperty(key)) {
  124. var tag = tags[key];
  125. ajax.processTag(tag);
  126. };
  127. };
  128. ajax.reloadTag = function(tag) {
  129. ajax.processTag(tag);
  130. };
  131. ajax.reloadTagById = function(id) {
  132. var tag = document.getElementById(id)
  133. if(tag != null) {
  134. ajax.reloadTag(tag);
  135. };
  136. };
  137. if(window.attachEvent) {
  138. window.attachEvent('onload', ajax.processTags);
  139. } else if(window.addEventListener) {
  140. window.addEventListener('load', ajax.processTags, false);
  141. };