ajax.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. // remote scripting library
  2. // (c) copyright 2005 modernmethod, inc
  3. var sajax_debug_mode = false;
  4. var sajax_request_type = "GET";
  5. /**
  6. * if sajax_debug_mode is true, this function outputs given the message into
  7. * the element with id = sajax_debug; if no such element exists in the document,
  8. * it is injected.
  9. */
  10. function sajax_debug(text) {
  11. if (!sajax_debug_mode) return false;
  12. var e= document.getElementById('sajax_debug');
  13. if (!e) {
  14. e= document.createElement("p");
  15. e.className= 'sajax_debug';
  16. e.id= 'sajax_debug';
  17. var b= document.getElementsByTagName("body")[0];
  18. if (b.firstChild) b.insertBefore(e, b.firstChild);
  19. else b.appendChild(e);
  20. }
  21. var m= document.createElement("div");
  22. m.appendChild( document.createTextNode( text ) );
  23. e.appendChild( m );
  24. return true;
  25. }
  26. /**
  27. * compatibility wrapper for creating a new XMLHttpRequest object.
  28. */
  29. function sajax_init_object() {
  30. sajax_debug("sajax_init_object() called..")
  31. var A;
  32. try {
  33. // Try the new style before ActiveX so we don't
  34. // unnecessarily trigger warnings in IE 7 when
  35. // set to prompt about ActiveX usage
  36. A = new XMLHttpRequest();
  37. } catch (e) {
  38. try {
  39. A=new ActiveXObject("Msxml2.XMLHTTP");
  40. } catch (e) {
  41. try {
  42. A=new ActiveXObject("Microsoft.XMLHTTP");
  43. } catch (oc) {
  44. A=null;
  45. }
  46. }
  47. }
  48. if (!A)
  49. sajax_debug("Could not create connection object.");
  50. return A;
  51. }
  52. /**
  53. * Perform an ajax call to mediawiki. Calls are handeled by AjaxDispatcher.php
  54. * func_name - the name of the function to call. Must be registered in $wgAjaxExportList
  55. * args - an array of arguments to that function
  56. * target - the target that will handle the result of the call. If this is a function,
  57. * if will be called with the XMLHttpRequest as a parameter; if it's an input
  58. * element, its value will be set to the resultText; if it's another type of
  59. * element, its innerHTML will be set to the resultText.
  60. *
  61. * Example:
  62. * sajax_do_call('doFoo', [1, 2, 3], document.getElementById("showFoo"));
  63. *
  64. * This will call the doFoo function via MediaWiki's AjaxDispatcher, with
  65. * (1, 2, 3) as the parameter list, and will show the result in the element
  66. * with id = showFoo
  67. */
  68. function sajax_do_call(func_name, args, target) {
  69. var i, x, n;
  70. var uri;
  71. var post_data;
  72. uri = wgServer +
  73. ((wgScript == null) ? (wgScriptPath + "/index.php") : wgScript) +
  74. "?action=ajax";
  75. if (sajax_request_type == "GET") {
  76. if (uri.indexOf("?") == -1)
  77. uri = uri + "?rs=" + encodeURIComponent(func_name);
  78. else
  79. uri = uri + "&rs=" + encodeURIComponent(func_name);
  80. for (i = 0; i < args.length; i++)
  81. uri = uri + "&rsargs[]=" + encodeURIComponent(args[i]);
  82. //uri = uri + "&rsrnd=" + new Date().getTime();
  83. post_data = null;
  84. } else {
  85. post_data = "rs=" + encodeURIComponent(func_name);
  86. for (i = 0; i < args.length; i++)
  87. post_data = post_data + "&rsargs[]=" + encodeURIComponent(args[i]);
  88. }
  89. x = sajax_init_object();
  90. if (!x) {
  91. alert("AJAX not supported");
  92. return false;
  93. }
  94. try {
  95. x.open(sajax_request_type, uri, true);
  96. } catch (e) {
  97. if (window.location.hostname == "localhost") {
  98. alert("Your browser blocks XMLHttpRequest to 'localhost', try using a real hostname for development/testing.");
  99. }
  100. throw e;
  101. }
  102. if (sajax_request_type == "POST") {
  103. x.setRequestHeader("Method", "POST " + uri + " HTTP/1.1");
  104. x.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  105. }
  106. x.setRequestHeader("Pragma", "cache=yes");
  107. x.setRequestHeader("Cache-Control", "no-transform");
  108. x.onreadystatechange = function() {
  109. if (x.readyState != 4)
  110. return;
  111. sajax_debug("received (" + x.status + " " + x.statusText + ") " + x.responseText);
  112. //if (x.status != 200)
  113. // alert("Error: " + x.status + " " + x.statusText + ": " + x.responseText);
  114. //else
  115. if ( typeof( target ) == 'function' ) {
  116. target( x );
  117. }
  118. else if ( typeof( target ) == 'object' ) {
  119. if ( target.tagName == 'INPUT' ) {
  120. if (x.status == 200) target.value= x.responseText;
  121. //else alert("Error: " + x.status + " " + x.statusText + " (" + x.responseText + ")");
  122. }
  123. else {
  124. if (x.status == 200) target.innerHTML = x.responseText;
  125. else target.innerHTML= "<div class='error'>Error: " + x.status + " " + x.statusText + " (" + x.responseText + ")</div>";
  126. }
  127. }
  128. else {
  129. alert("bad target for sajax_do_call: not a function or object: " + target);
  130. }
  131. return;
  132. }
  133. sajax_debug(func_name + " uri = " + uri + " / post = " + post_data);
  134. x.send(post_data);
  135. sajax_debug(func_name + " waiting..");
  136. delete x;
  137. return true;
  138. }
  139. /**
  140. * @return boolean whether the browser supports XMLHttpRequest
  141. */
  142. function wfSupportsAjax() {
  143. var request = sajax_init_object();
  144. var supportsAjax = request ? true : false;
  145. delete request;
  146. return supportsAjax;
  147. }