2 * Remote Scripting Library
3 * Copyright 2005 modernmethod, inc
4 * Under the open source BSD license
5 * http://www.modernmethod.com/sajax/
8 /*jscs:disable requireCamelCaseOrUpperCaseIdentifiers */
13 * if sajax_debug_mode is true, this function outputs given the message into
14 * the element with id = sajax_debug; if no such element exists in the document,
17 function debug( text ) {
18 if ( !window.sajax_debug_mode ) {
23 e = document.getElementById( 'sajax_debug' );
26 e = document.createElement( 'p' );
27 e.className = 'sajax_debug';
30 b = document.getElementsByTagName( 'body' )[0];
33 b.insertBefore( e, b.firstChild );
39 m = document.createElement( 'div' );
40 m.appendChild( document.createTextNode( text ) );
48 * Compatibility wrapper for creating a new XMLHttpRequest object.
50 function createXhr() {
51 debug( 'sajax_init_object() called..' );
54 // Try the new style before ActiveX so we don't
55 // unnecessarily trigger warnings in IE 7 when
56 // set to prompt about ActiveX usage
57 a = new XMLHttpRequest();
60 a = new window.ActiveXObject( 'Msxml2.XMLHTTP' );
63 a = new window.ActiveXObject( 'Microsoft.XMLHTTP' );
70 debug( 'Could not create connection object.' );
77 * Perform an AJAX call to MediaWiki. Calls are handled by AjaxDispatcher.php
78 * func_name - the name of the function to call. Must be registered in $wgAjaxExportList
79 * args - an array of arguments to that function
80 * target - the target that will handle the result of the call. If this is a function,
81 * if will be called with the XMLHttpRequest as a parameter; if it's an input
82 * element, its value will be set to the resultText; if it's another type of
83 * element, its innerHTML will be set to the resultText.
86 * sajax_do_call( 'doFoo', [1, 2, 3], document.getElementById( 'showFoo' ) );
88 * This will call the doFoo function via MediaWiki's AjaxDispatcher, with
89 * (1, 2, 3) as the parameter list, and will show the result in the element
92 function doAjaxRequest( func_name, args, target ) {
93 var i, x, uri, post_data;
94 uri = mw.util.wikiScript() + '?action=ajax';
95 if ( window.sajax_request_type === 'GET' ) {
96 if ( uri.indexOf( '?' ) === -1 ) {
97 uri = uri + '?rs=' + encodeURIComponent( func_name );
99 uri = uri + '&rs=' + encodeURIComponent( func_name );
101 for ( i = 0; i < args.length; i++ ) {
102 uri = uri + '&rsargs[]=' + encodeURIComponent( args[i] );
104 //uri = uri + '&rsrnd=' + new Date().getTime();
107 post_data = 'rs=' + encodeURIComponent( func_name );
108 for ( i = 0; i < args.length; i++ ) {
109 post_data = post_data + '&rsargs[]=' + encodeURIComponent( args[i] );
114 alert( 'AJAX not supported' );
119 x.open( window.sajax_request_type, uri, true );
121 if ( location.hostname === 'localhost' ) {
122 alert( 'Your browser blocks XMLHttpRequest to "localhost", try using a real hostname for development/testing.' );
126 if ( window.sajax_request_type === 'POST' ) {
127 x.setRequestHeader( 'Method', 'POST ' + uri + ' HTTP/1.1' );
128 x.setRequestHeader( 'Content-Type', 'application/x-www-form-urlencoded' );
130 x.setRequestHeader( 'Pragma', 'cache=yes' );
131 x.setRequestHeader( 'Cache-Control', 'no-transform' );
132 x.onreadystatechange = function () {
133 if ( x.readyState !== 4 ) {
137 debug( 'received (' + x.status + ' ' + x.statusText + ') ' + x.responseText );
139 //if ( x.status != 200 )
140 // alert( 'Error: ' + x.status + ' ' + x.statusText + ': ' + x.responseText );
143 if ( typeof target === 'function' ) {
145 } else if ( typeof target === 'object' ) {
146 if ( target.tagName === 'INPUT' ) {
147 if ( x.status === 200 ) {
148 target.value = x.responseText;
150 //else alert( 'Error: ' + x.status + ' ' + x.statusText + ' (' + x.responseText + ')' );
152 if ( x.status === 200 ) {
153 target.innerHTML = x.responseText;
155 target.innerHTML = '<div class="error">Error: ' + x.status +
156 ' ' + x.statusText + ' (' + x.responseText + ')</div>';
160 alert( 'Bad target for sajax_do_call: not a function or object: ' + target );
164 debug( func_name + ' uri = ' + uri + ' / post = ' + post_data );
166 debug( func_name + ' waiting..' );
172 * @return {boolean} Whether the browser supports AJAX
174 function wfSupportsAjax() {
175 var request = createXhr(),
176 supportsAjax = request ? true : false;
182 // Expose + Mark as deprecated
183 var deprecationNotice = 'Sajax is deprecated, use jQuery.ajax or mediawiki.api instead.';
186 mw.log.deprecate( window, 'sajax_debug_mode', false, deprecationNotice );
187 mw.log.deprecate( window, 'sajax_request_type', 'GET', deprecationNotice );
189 mw.log.deprecate( window, 'sajax_debug', debug, deprecationNotice );
190 mw.log.deprecate( window, 'sajax_init_object', createXhr, deprecationNotice );
191 mw.log.deprecate( window, 'sajax_do_call', doAjaxRequest, deprecationNotice );
192 mw.log.deprecate( window, 'wfSupportsAjax', wfSupportsAjax, deprecationNotice );