ajax.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. A=new ActiveXObject("Msxml2.XMLHTTP");
  34. } catch (e) {
  35. try {
  36. A=new ActiveXObject("Microsoft.XMLHTTP");
  37. } catch (oc) {
  38. A=null;
  39. }
  40. }
  41. if(!A && typeof XMLHttpRequest != "undefined")
  42. A = new XMLHttpRequest();
  43. if (!A)
  44. sajax_debug("Could not create connection object.");
  45. return A;
  46. }
  47. /**
  48. * Perform an ajax call to mediawiki. Calls are handeled by AjaxDispatcher.php
  49. * func_name - the name of the function to call. Must be registered in $wgAjaxExportList
  50. * args - an array of arguments to that function
  51. * target - the target that will handle the result of the call. If this is a function,
  52. * if will be called with the XMLHttpRequest as a parameter; if it's an input
  53. * element, its value will be set to the resultText; if it's another type of
  54. * element, its innerHTML will be set to the resultText.
  55. *
  56. * Example:
  57. * sajax_do_call('doFoo', [1, 2, 3], document.getElementById("showFoo"));
  58. *
  59. * This will call the doFoo function via MediaWiki's AjaxDispatcher, with
  60. * (1, 2, 3) as the parameter list, and will show the result in the element
  61. * with id = showFoo
  62. */
  63. function sajax_do_call(func_name, args, target) {
  64. var i, x, n;
  65. var uri;
  66. var post_data;
  67. uri = wgServer + wgScriptPath + "/index.php?action=ajax";
  68. if (sajax_request_type == "GET") {
  69. if (uri.indexOf("?") == -1)
  70. uri = uri + "?rs=" + encodeURIComponent(func_name);
  71. else
  72. uri = uri + "&rs=" + encodeURIComponent(func_name);
  73. for (i = 0; i < args.length; i++)
  74. uri = uri + "&rsargs[]=" + encodeURIComponent(args[i]);
  75. //uri = uri + "&rsrnd=" + new Date().getTime();
  76. post_data = null;
  77. } else {
  78. post_data = "rs=" + encodeURIComponent(func_name);
  79. for (i = 0; i < args.length; i++)
  80. post_data = post_data + "&rsargs[]=" + encodeURIComponent(args[i]);
  81. }
  82. x = sajax_init_object();
  83. if (!x) {
  84. alert("AJAX not supported");
  85. return false;
  86. }
  87. try {
  88. x.open(sajax_request_type, uri, true);
  89. } catch (e) {
  90. if (window.location.hostname == "localhost") {
  91. alert("Your browser blocks XMLHttpRequest to 'localhost', try using a real hostname for development/testing.");
  92. }
  93. throw e;
  94. }
  95. if (sajax_request_type == "POST") {
  96. x.setRequestHeader("Method", "POST " + uri + " HTTP/1.1");
  97. x.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  98. }
  99. x.setRequestHeader("Pragma", "cache=yes");
  100. x.setRequestHeader("Cache-Control", "no-transform");
  101. x.onreadystatechange = function() {
  102. if (x.readyState != 4)
  103. return;
  104. sajax_debug("received (" + x.status + " " + x.statusText + ") " + x.responseText);
  105. //if (x.status != 200)
  106. // alert("Error: " + x.status + " " + x.statusText + ": " + x.responseText);
  107. //else
  108. if ( typeof( target ) == 'function' ) {
  109. target( x );
  110. }
  111. else if ( typeof( target ) == 'object' ) {
  112. if ( target.tagName == 'INPUT' ) {
  113. if (x.status == 200) target.value= x.responseText;
  114. //else alert("Error: " + x.status + " " + x.statusText + " (" + x.responseText + ")");
  115. }
  116. else {
  117. if (x.status == 200) target.innerHTML = x.responseText;
  118. else target.innerHTML= "<div class='error'>Error: " + x.status + " " + x.statusText + " (" + x.responseText + ")</div>";
  119. }
  120. }
  121. else {
  122. alert("bad target for sajax_do_call: not a function or object: " + target);
  123. }
  124. return;
  125. }
  126. sajax_debug(func_name + " uri = " + uri + " / post = " + post_data);
  127. x.send(post_data);
  128. sajax_debug(func_name + " waiting..");
  129. delete x;
  130. return true;
  131. }