ajax.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /* $Id: ajax.js 3008 2007-05-17 17:07:11Z danez $ */
  2. // fields
  3. var ajax_debug = false;
  4. var ajax_useXML = false;
  5. var ajax_txtDelim = ";";
  6. var ajax_updateUrl = "stats.php";
  7. var ajax_updateParams = "";
  8. var ajax_updateTimer = 5000;
  9. var ajax_updateState = 0; // 0 = update off; 1 = update on
  10. var ajax_httpRequest = false;
  11. var ajax_requestOpen = false;
  12. /**
  13. * get http-request-instance
  14. */
  15. function ajax_getHttpRequest() {
  16. _httpRequest = false;
  17. if (window.XMLHttpRequest) { // Mozilla, Safari,...
  18. _httpRequest = new XMLHttpRequest();
  19. if (_httpRequest.overrideMimeType)
  20. _httpRequest.overrideMimeType('text/xml');
  21. } else if (window.ActiveXObject) { // IE
  22. try {
  23. _httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
  24. } catch (e) {
  25. try {
  26. _httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
  27. } catch (e) {}
  28. }
  29. }
  30. if (!_httpRequest) {
  31. if (ajax_debug)
  32. alert('Error : cant create XMLHTTP-instance');
  33. return false;
  34. }
  35. return _httpRequest;
  36. }
  37. /**
  38. * ajax_update
  39. */
  40. function ajax_update() {
  41. if (ajax_updateState == 1) {
  42. try {
  43. if (!ajax_httpRequest)
  44. ajax_httpRequest = ajax_getHttpRequest();
  45. else
  46. ajax_httpRequest.abort();
  47. if(ajax_httpRequest && (ajax_httpRequest.readyState == 4 || ajax_httpRequest.readyState == 0))
  48. {
  49. ajax_httpRequest.onreadystatechange = ajax_updateCallback;
  50. ajax_httpRequest.open('GET', ajax_updateUrl + ajax_updateParams, true);
  51. ajax_httpRequest.send(null);
  52. ajax_requestOpen = true;
  53. }
  54. } catch (ajaxception) {
  55. if (ajax_debug)
  56. alert('name : ' + ajaxception.name + ' | message:' + ajaxception.message);
  57. ajax_updateState = 0;
  58. }
  59. }
  60. }
  61. /**
  62. * update-callback
  63. */
  64. function ajax_updateCallback() {
  65. if (ajax_httpRequest.readyState == 4 && ajax_requestOpen === true) {
  66. if (ajax_httpRequest.status == 200) {
  67. if (ajax_useXML)
  68. ajax_processXML(ajax_httpRequest.responseXML);
  69. else
  70. ajax_processText(ajax_httpRequest.responseText);
  71. ajax_requestOpen = false;
  72. } else {
  73. if (ajax_debug)
  74. alert('Error in Request :' + ajax_httpRequest.status);
  75. }
  76. }
  77. }