1 // remote scripting library
2 // (c) copyright 2005 modernmethod, inc
3 var sajax_debug_mode
= false;
4 var sajax_request_type
= 'GET';
7 * if sajax_debug_mode is true, this function outputs given the message into
8 * the element with id = sajax_debug; if no such element exists in the document,
11 function sajax_debug( text
) {
12 if ( !sajax_debug_mode
) {
16 var e
= document
.getElementById( 'sajax_debug' );
19 e
= document
.createElement( 'p' );
20 e
.className
= 'sajax_debug';
23 var b
= document
.getElementsByTagName( 'body' )[0];
26 b
.insertBefore( e
, b
.firstChild
);
32 var m
= document
.createElement( 'div' );
33 m
.appendChild( document
.createTextNode( text
) );
41 * Compatibility wrapper for creating a new XMLHttpRequest object.
43 function sajax_init_object() {
44 sajax_debug( 'sajax_init_object() called..' );
47 // Try the new style before ActiveX so we don't
48 // unnecessarily trigger warnings in IE 7 when
49 // set to prompt about ActiveX usage
50 A
= new XMLHttpRequest();
53 A
= new ActiveXObject( 'Msxml2.XMLHTTP' );
56 A
= new ActiveXObject( 'Microsoft.XMLHTTP' );
63 sajax_debug( 'Could not create connection object.' );
70 * Perform an AJAX call to MediaWiki. Calls are handled by AjaxDispatcher.php
71 * func_name - the name of the function to call. Must be registered in $wgAjaxExportList
72 * args - an array of arguments to that function
73 * target - the target that will handle the result of the call. If this is a function,
74 * if will be called with the XMLHttpRequest as a parameter; if it's an input
75 * element, its value will be set to the resultText; if it's another type of
76 * element, its innerHTML will be set to the resultText.
79 * sajax_do_call( 'doFoo', [1, 2, 3], document.getElementById( 'showFoo' ) );
81 * This will call the doFoo function via MediaWiki's AjaxDispatcher, with
82 * (1, 2, 3) as the parameter list, and will show the result in the element
85 function sajax_do_call( func_name
, args
, target
) {
90 ( ( wgScript
== null ) ? ( wgScriptPath
+ '/index.php' ) : wgScript
) +
92 if ( sajax_request_type
== 'GET' ) {
93 if ( uri
.indexOf( '?' ) == -1 ) {
94 uri
= uri
+ '?rs=' + encodeURIComponent( func_name
);
96 uri
= uri
+ '&rs=' + encodeURIComponent( func_name
);
98 for ( i
= 0; i
< args
.length
; i
++ ) {
99 uri
= uri
+ '&rsargs[]=' + encodeURIComponent( args
[i
] );
101 //uri = uri + '&rsrnd=' + new Date().getTime();
104 post_data
= 'rs=' + encodeURIComponent( func_name
);
105 for ( i
= 0; i
< args
.length
; i
++ ) {
106 post_data
= post_data
+ '&rsargs[]=' + encodeURIComponent( args
[i
] );
109 x
= sajax_init_object();
111 alert( 'AJAX not supported' );
116 x
.open( sajax_request_type
, uri
, true );
118 if ( window
.location
.hostname
== 'localhost' ) {
119 alert( "Your browser blocks XMLHttpRequest to 'localhost', try using a real hostname for development/testing." );
123 if ( sajax_request_type
== 'POST' ) {
124 x
.setRequestHeader( 'Method', 'POST ' + uri
+ ' HTTP/1.1' );
125 x
.setRequestHeader( 'Content-Type', 'application/x-www-form-urlencoded' );
127 x
.setRequestHeader( 'Pragma', 'cache=yes' );
128 x
.setRequestHeader( 'Cache-Control', 'no-transform' );
129 x
.onreadystatechange = function() {
130 if ( x
.readyState
!= 4 ) {
134 sajax_debug( 'received (' + x
.status
+ ' ' + x
.statusText
+ ') ' + x
.responseText
);
136 //if ( x.status != 200 )
137 // alert( 'Error: ' + x.status + ' ' + x.statusText + ': ' + x.responseText );
140 if ( typeof( target
) == 'function' ) {
142 } else if ( typeof( target
) == 'object' ) {
143 if ( target
.tagName
== 'INPUT' ) {
144 if ( x
.status
== 200 ) {
145 target
.value
= x
.responseText
;
147 //else alert( 'Error: ' + x.status + ' ' + x.statusText + ' (' + x.responseText + ')' );
149 if ( x
.status
== 200 ) {
150 target
.innerHTML
= x
.responseText
;
152 target
.innerHTML
= '<div class="error">Error: ' + x
.status
+
153 ' ' + x
.statusText
+ ' (' + x
.responseText
+ ')</div>';
157 alert( 'bad target for sajax_do_call: not a function or object: ' + target
);
163 sajax_debug( func_name
+ ' uri = ' + uri
+ ' / post = ' + post_data
);
165 sajax_debug( func_name
+ ' waiting..' );
172 * @return boolean whether the browser supports XMLHttpRequest
174 function wfSupportsAjax() {
175 var request
= sajax_init_object();
176 var supportsAjax
= request
? true : false;