2 * Legacy emulation for the now depricated ajax.js
4 * Original licensing information:
5 * remote scripting library
6 * (c) copyright 2005 modernmethod, inc
8 * Ported by: Trevor Parscal
13 $.extend( mw
.legacy
, {
15 /* Global Variables */
17 'sajax_debug_mode': false,
18 'sajax_debug_mode': "GET",
23 * If sajax_debug_mode is true, this function outputs given the message into the element with id = sajax_debug; if no
24 * such element exists in the document, it is injected
26 * @param string text debug message to append to log
27 * @return boolean true when in debug mode, false when not
29 'sajax_debug': function( text
) {
30 if ( mw
.legacy
.sajax_debug_mode
) {
31 var $e
= $( '#sajax_debug' );
33 $e
= $( '<p class="sajax_debug" id="sajax_debug"></p>' ).prependTo( $( 'body' ) );
35 $e
.append( $( '<div></div>' ).text( text
) );
41 * Gets an XMLHttpRequest or equivilant ActiveXObject
43 * @reuturn mixed request object on success, boolean false on failure
45 'sajax_init_object': function() {
46 mw
.legacy
.sajax_debug( 'sajax_init_object() called..' );
49 // Try the "new" style before ActiveX so we don't unnecessarily trigger warnings in IE 7 when the user's
50 // security settings are set to prompt about ActiveX usage
51 request
= new XMLHttpRequest();
54 request
= new ActiveXObject( 'Msxml2.XMLHTTP' );
57 request
= new ActiveXObject( 'Microsoft.XMLHTTP' );
64 mw
.legacy
.sajax_debug( 'Could not create connection object.' );
69 * Performs an ajax call to mediawiki. Calls are handeled by AjaxDispatcher.php
71 * @param string method name of the function to call. Must be registered in $wgAjaxExportList
72 * @param array arguments arguments to that function
73 * @param mixed target the target that will handle the result of the call. If this is a function, if will be called
74 * with the XMLHttpRequest as a parameter; if it's an input element, its value will be set to the resultText; if
75 * it's another type of element, its innerHTML will be set to the resultText.
78 * // This will call the doFoo function via MediaWiki's AjaxDispatcher, with (1, 2, 3) as the parameter list,
79 * // and will show the result in the element with id = showFoo
80 * sajax_do_call( 'doFoo', [1, 2, 3], document.getElementById( 'showFoo' ) );
82 'sajax_do_call': function( method
, arguments
, target
) {
84 var uri
= mw
.legacy
.wgServer
+
85 ( ( mw
.legacy
.wgScript
== null ) ? ( mw
.legacy
.wgScriptPath
+ '/index.php' ) : mw
.legacy
.wgScript
) +
87 if ( mw
.legacy
.sajax_request_type
== 'GET' ) {
88 if ( uri
.indexOf( '?' ) == -1 ) {
89 uri
= uri
+ '?rs=' + encodeURIComponent( method
);
91 uri
= uri
+ '&rs=' + encodeURIComponent( method
);
93 for ( var i
= 0; i
< arguments
.length
; i
++ ) {
94 uri
= uri
+ '&rsargs[]=' + encodeURIComponent( arguments
[i
] );
98 post_data
= 'rs=' + encodeURIComponent( method
);
99 for ( var i
= 0; i
< arguments
.length
; i
++ ) {
100 post_data
= post_data
+ '&rsargs[]=' + encodeURIComponent( arguments
[i
] );
103 var request
= mw
.legacy
.sajax_init_object();
105 alert( 'AJAX not supported' );
109 request
.open( mw
.legacy
.sajax_request_type
, uri
, true );
111 if ( window
.location
.hostname
== "localhost" ) {
113 'Your browser blocks XMLHttpRequest to \'localhost\', ' +
114 'try using a real hostname for development/testing.'
119 if ( mw
.legacy
.sajax_request_type
== 'POST' ) {
120 request
.setRequestHeader( 'Method', 'POST ' + uri
+ ' HTTP/1.1' );
121 request
.setRequestHeader( 'Content-Type', 'application/x-www-form-urlencoded' );
123 request
.setRequestHeader( 'Pragma', 'cache=yes' );
124 request
.setRequestHeader( 'Cache-Control', 'no-transform' );
125 request
.onreadystatechange = function() {
126 if ( request
.readyState
!= 4 ) {
129 mw
.legacy
.sajax_debug(
130 'received (' + request
.status
+ ' ' + request
.statusText
+ ') ' + request
.responseText
132 if ( typeof( target
) == 'function' ) {
134 } else if ( typeof( target
) == 'object' ) {
135 $target
= $( target
);
136 if ( $target
.is( 'input' ) ) {
137 if ( request
.status
== 200 ) {
141 if ( request
.status
== 200 ) {
142 $target
.html( request
.responseText
);
145 '<div class="error">' +
146 'Error: ' + request
.status
+ ' ' + request
.statusText
+
147 ' (' + request
.responseText
+ ')' +
153 alert( 'Bad target for sajax_do_call: not a function or object: ' + target
);
157 mw
.legacy
.sajax_debug( method
+ ' uri = ' + uri
+ ' / post = ' + post_data
);
158 request
.send( post_data
);
159 mw
.legacy
.sajax_debug( method
+ ' waiting..' );
164 * Ajax compatibility test
166 * @return boolean whether the browser supports XMLHttpRequest
168 'wfSupportsAjax': function() {
169 var request
= mw
.legacy
.sajax_init_object();
170 var result
= request
? true : false;