2 * Remote Scripting Library
3 * Copyright 2005 modernmethod, inc
4 * Under the open source BSD license
5 * http://www.modernmethod.com/sajax/
8 /*jshint camelcase:false, onevar:false */
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 ) {
22 var e = document.getElementById( 'sajax_debug' );
25 e = document.createElement( 'p' );
26 e.className = 'sajax_debug';
29 var b = document.getElementsByTagName( 'body' )[0];
32 b.insertBefore( e, b.firstChild );
38 var m = document.createElement( 'div' );
39 m.appendChild( document.createTextNode( text ) );
47 * Compatibility wrapper for creating a new XMLHttpRequest object.
49 function createXhr() {
50 debug( 'sajax_init_object() called..' );
53 // Try the new style before ActiveX so we don't
54 // unnecessarily trigger warnings in IE 7 when
55 // set to prompt about ActiveX usage
56 a = new XMLHttpRequest();
59 a = new window.ActiveXObject( 'Msxml2.XMLHTTP' );
62 a = new window.ActiveXObject( 'Microsoft.XMLHTTP' );
69 debug( 'Could not create connection object.' );
76 * Perform an AJAX call to MediaWiki. Calls are handled by AjaxDispatcher.php
77 * func_name - the name of the function to call. Must be registered in $wgAjaxExportList
78 * args - an array of arguments to that function
79 * target - the target that will handle the result of the call. If this is a function,
80 * if will be called with the XMLHttpRequest as a parameter; if it's an input
81 * element, its value will be set to the resultText; if it's another type of
82 * element, its innerHTML will be set to the resultText.
85 * sajax_do_call( 'doFoo', [1, 2, 3], document.getElementById( 'showFoo' ) );
87 * This will call the doFoo function via MediaWiki's AjaxDispatcher, with
88 * (1, 2, 3) as the parameter list, and will show the result in the element
91 function doAjaxRequest( func_name, args, target ) {
95 uri = mw.util.wikiScript() + '?action=ajax';
96 if ( window.sajax_request_type === 'GET' ) {
97 if ( uri.indexOf( '?' ) === -1 ) {
98 uri = uri + '?rs=' + encodeURIComponent( func_name );
100 uri = uri + '&rs=' + encodeURIComponent( func_name );
102 for ( i = 0; i < args.length; i++ ) {
103 uri = uri + '&rsargs[]=' + encodeURIComponent( args[i] );
105 //uri = uri + '&rsrnd=' + new Date().getTime();
108 post_data = 'rs=' + encodeURIComponent( func_name );
109 for ( i = 0; i < args.length; i++ ) {
110 post_data = post_data + '&rsargs[]=' + encodeURIComponent( args[i] );
115 alert( 'AJAX not supported' );
120 x.open( window.sajax_request_type, uri, true );
122 if ( location.hostname === 'localhost' ) {
123 alert( 'Your browser blocks XMLHttpRequest to "localhost", try using a real hostname for development/testing.' );
127 if ( window.sajax_request_type === 'POST' ) {
128 x.setRequestHeader( 'Method', 'POST ' + uri + ' HTTP/1.1' );
129 x.setRequestHeader( 'Content-Type', 'application/x-www-form-urlencoded' );
131 x.setRequestHeader( 'Pragma', 'cache=yes' );
132 x.setRequestHeader( 'Cache-Control', 'no-transform' );
133 x.onreadystatechange = function () {
134 if ( x.readyState !== 4 ) {
138 debug( 'received (' + x.status + ' ' + x.statusText + ') ' + x.responseText );
140 //if ( x.status != 200 )
141 // alert( 'Error: ' + x.status + ' ' + x.statusText + ': ' + x.responseText );
144 if ( typeof target === 'function' ) {
146 } else if ( typeof target === 'object' ) {
147 if ( target.tagName === 'INPUT' ) {
148 if ( x.status === 200 ) {
149 target.value= x.responseText;
151 //else alert( 'Error: ' + x.status + ' ' + x.statusText + ' (' + x.responseText + ')' );
153 if ( x.status === 200 ) {
154 target.innerHTML = x.responseText;
156 target.innerHTML = '<div class="error">Error: ' + x.status +
157 ' ' + x.statusText + ' (' + x.responseText + ')</div>';
161 alert( 'Bad target for sajax_do_call: not a function or object: ' + target );
165 debug( func_name + ' uri = ' + uri + ' / post = ' + post_data );
167 debug( func_name + ' waiting..' );
173 * @return {boolean} Whether the browser supports AJAX
175 function wfSupportsAjax() {
176 var request = createXhr();
177 var 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 );