1 // remote scripting library
2 // (c) copyright 2005 modernmethod, inc
3 window
.sajax_debug_mode
= false;
4 window
.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 window
.sajax_debug = function(text
) {
12 if (!sajax_debug_mode
) return false;
14 var e
= document
.getElementById( 'sajax_debug' );
17 e
= document
.createElement( 'p' );
18 e
.className
= 'sajax_debug';
21 var b
= document
.getElementsByTagName( 'body' )[0];
24 b
.insertBefore( e
, b
.firstChild
);
30 var m
= document
.createElement( 'div' );
31 m
.appendChild( document
.createTextNode( text
) );
39 * Compatibility wrapper for creating a new XMLHttpRequest object.
41 window
.sajax_init_object = function() {
42 sajax_debug( 'sajax_init_object() called..' );
45 // Try the new style before ActiveX so we don't
46 // unnecessarily trigger warnings in IE 7 when
47 // set to prompt about ActiveX usage
48 A
= new XMLHttpRequest();
51 A
= new ActiveXObject( 'Msxml2.XMLHTTP' );
54 A
= new ActiveXObject( 'Microsoft.XMLHTTP' );
61 sajax_debug( 'Could not create connection object.' );
68 * Perform an AJAX call to MediaWiki. Calls are handled by AjaxDispatcher.php
69 * func_name - the name of the function to call. Must be registered in $wgAjaxExportList
70 * args - an array of arguments to that function
71 * target - the target that will handle the result of the call. If this is a function,
72 * if will be called with the XMLHttpRequest as a parameter; if it's an input
73 * element, its value will be set to the resultText; if it's another type of
74 * element, its innerHTML will be set to the resultText.
77 * sajax_do_call( 'doFoo', [1, 2, 3], document.getElementById( 'showFoo' ) );
79 * This will call the doFoo function via MediaWiki's AjaxDispatcher, with
80 * (1, 2, 3) as the parameter list, and will show the result in the element
83 window
.sajax_do_call = function(func_name
, args
, target
) {
88 ( ( wgScript
== null ) ? ( wgScriptPath
+ '/index.php' ) : wgScript
) +
90 if ( sajax_request_type
== 'GET' ) {
91 if ( uri
.indexOf( '?' ) == -1 ) {
92 uri
= uri
+ '?rs=' + encodeURIComponent( func_name
);
94 uri
= uri
+ '&rs=' + encodeURIComponent( func_name
);
96 for ( i
= 0; i
< args
.length
; i
++ ) {
97 uri
= uri
+ '&rsargs[]=' + encodeURIComponent( args
[i
] );
99 //uri = uri + '&rsrnd=' + new Date().getTime();
102 post_data
= 'rs=' + encodeURIComponent( func_name
);
103 for ( i
= 0; i
< args
.length
; i
++ ) {
104 post_data
= post_data
+ '&rsargs[]=' + encodeURIComponent( args
[i
] );
107 x
= sajax_init_object();
109 alert( 'AJAX not supported' );
114 x
.open( sajax_request_type
, uri
, true );
116 if ( window
.location
.hostname
== 'localhost' ) {
117 alert( "Your browser blocks XMLHttpRequest to 'localhost', try using a real hostname for development/testing." );
121 if ( sajax_request_type
== 'POST' ) {
122 x
.setRequestHeader( 'Method', 'POST ' + uri
+ ' HTTP/1.1' );
123 x
.setRequestHeader( 'Content-Type', 'application/x-www-form-urlencoded' );
125 x
.setRequestHeader( 'Pragma', 'cache=yes' );
126 x
.setRequestHeader( 'Cache-Control', 'no-transform' );
127 x
.onreadystatechange = function() {
128 if ( x
.readyState
!= 4 ) {
132 sajax_debug( 'received (' + x
.status
+ ' ' + x
.statusText
+ ') ' + x
.responseText
);
134 //if ( x.status != 200 )
135 // alert( 'Error: ' + x.status + ' ' + x.statusText + ': ' + x.responseText );
138 if ( typeof( target
) == 'function' ) {
140 } else if ( typeof( target
) == 'object' ) {
141 if ( target
.tagName
== 'INPUT' ) {
142 if ( x
.status
== 200 ) {
143 target
.value
= x
.responseText
;
145 //else alert( 'Error: ' + x.status + ' ' + x.statusText + ' (' + x.responseText + ')' );
147 if ( x
.status
== 200 ) {
148 target
.innerHTML
= x
.responseText
;
150 target
.innerHTML
= '<div class="error">Error: ' + x
.status
+
151 ' ' + x
.statusText
+ ' (' + x
.responseText
+ ')</div>';
155 alert( 'bad target for sajax_do_call: not a function or object: ' + target
);
159 sajax_debug( func_name
+ ' uri = ' + uri
+ ' / post = ' + post_data
);
161 sajax_debug( func_name
+ ' waiting..' );
168 * @return boolean whether the browser supports XMLHttpRequest
170 window
.wfSupportsAjax = function() {
171 var request
= sajax_init_object();
172 var supportsAjax
= request
? true : false;