ajax.js 4.9 KB

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