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
) {
87 uri
= mw
.util
.wikiScript() + '?action=ajax';
88 if ( sajax_request_type
== 'GET' ) {
89 if ( uri
.indexOf( '?' ) == -1 ) {
90 uri
= uri
+ '?rs=' + encodeURIComponent( func_name
);
92 uri
= uri
+ '&rs=' + encodeURIComponent( func_name
);
94 for ( i
= 0; i
< args
.length
; i
++ ) {
95 uri
= uri
+ '&rsargs[]=' + encodeURIComponent( args
[i
] );
97 //uri = uri + '&rsrnd=' + new Date().getTime();
100 post_data
= 'rs=' + encodeURIComponent( func_name
);
101 for ( i
= 0; i
< args
.length
; i
++ ) {
102 post_data
= post_data
+ '&rsargs[]=' + encodeURIComponent( args
[i
] );
105 x
= sajax_init_object();
107 alert( 'AJAX not supported' );
112 x
.open( sajax_request_type
, uri
, true );
114 if ( window
.location
.hostname
== 'localhost' ) {
115 alert( "Your browser blocks XMLHttpRequest to 'localhost', try using a real hostname for development/testing." );
119 if ( sajax_request_type
== 'POST' ) {
120 x
.setRequestHeader( 'Method', 'POST ' + uri
+ ' HTTP/1.1' );
121 x
.setRequestHeader( 'Content-Type', 'application/x-www-form-urlencoded' );
123 x
.setRequestHeader( 'Pragma', 'cache=yes' );
124 x
.setRequestHeader( 'Cache-Control', 'no-transform' );
125 x
.onreadystatechange = function() {
126 if ( x
.readyState
!= 4 ) {
130 sajax_debug( 'received (' + x
.status
+ ' ' + x
.statusText
+ ') ' + x
.responseText
);
132 //if ( x.status != 200 )
133 // alert( 'Error: ' + x.status + ' ' + x.statusText + ': ' + x.responseText );
136 if ( typeof( target
) == 'function' ) {
138 } else if ( typeof( target
) == 'object' ) {
139 if ( target
.tagName
== 'INPUT' ) {
140 if ( x
.status
== 200 ) {
141 target
.value
= x
.responseText
;
143 //else alert( 'Error: ' + x.status + ' ' + x.statusText + ' (' + x.responseText + ')' );
145 if ( x
.status
== 200 ) {
146 target
.innerHTML
= x
.responseText
;
148 target
.innerHTML
= '<div class="error">Error: ' + x
.status
+
149 ' ' + x
.statusText
+ ' (' + x
.responseText
+ ')</div>';
153 alert( 'bad target for sajax_do_call: not a function or object: ' + target
);
157 sajax_debug( func_name
+ ' uri = ' + uri
+ ' / post = ' + post_data
);
159 sajax_debug( func_name
+ ' waiting..' );
166 * @return boolean whether the browser supports XMLHttpRequest
168 window
.wfSupportsAjax = function() {
169 var request
= sajax_init_object();
170 var supportsAjax
= request
? true : false;