Localisation updates from http://translatewiki.net.
[mediawiki.git] / skins / common / ajax.js
blob121f9d12f163e3fbb7161c954c110f4d03450300
1 // remote scripting library
2 // (c) copyright 2005 modernmethod, inc
3 window.sajax_debug_mode = false;
4 window.sajax_request_type = 'GET';
6 /**
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,
9 * it is injected.
11 window.sajax_debug = function(text) {
12 if (!sajax_debug_mode) return false;
14 var e = document.getElementById( 'sajax_debug' );
16 if ( !e ) {
17 e = document.createElement( 'p' );
18 e.className = 'sajax_debug';
19 e.id = 'sajax_debug';
21 var b = document.getElementsByTagName( 'body' )[0];
23 if ( b.firstChild ) {
24 b.insertBefore( e, b.firstChild );
25 } else {
26 b.appendChild( e );
30 var m = document.createElement( 'div' );
31 m.appendChild( document.createTextNode( text ) );
33 e.appendChild( m );
35 return true;
38 /**
39 * Compatibility wrapper for creating a new XMLHttpRequest object.
41 window.sajax_init_object = function() {
42 sajax_debug( 'sajax_init_object() called..' );
43 var A;
44 try {
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();
49 } catch ( e ) {
50 try {
51 A = new ActiveXObject( 'Msxml2.XMLHTTP' );
52 } catch ( e ) {
53 try {
54 A = new ActiveXObject( 'Microsoft.XMLHTTP' );
55 } catch ( oc ) {
56 A = null;
60 if ( !A ) {
61 sajax_debug( 'Could not create connection object.' );
64 return A;
67 /**
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.
76 * Example:
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
81 * with id = showFoo
83 window.sajax_do_call = function(func_name, args, target) {
84 var i, x, n;
85 var uri;
86 var post_data;
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 );
91 } else {
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();
98 post_data = null;
99 } else {
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();
106 if ( !x ) {
107 alert( 'AJAX not supported' );
108 return false;
111 try {
112 x.open( sajax_request_type, uri, true );
113 } catch ( e ) {
114 if ( window.location.hostname == 'localhost' ) {
115 alert( "Your browser blocks XMLHttpRequest to 'localhost', try using a real hostname for development/testing." );
117 throw e;
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 ) {
127 return;
130 sajax_debug( 'received (' + x.status + ' ' + x.statusText + ') ' + x.responseText );
132 //if ( x.status != 200 )
133 // alert( 'Error: ' + x.status + ' ' + x.statusText + ': ' + x.responseText );
134 //else
136 if ( typeof( target ) == 'function' ) {
137 target( x );
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 + ')' );
144 } else {
145 if ( x.status == 200 ) {
146 target.innerHTML = x.responseText;
147 } else {
148 target.innerHTML = '<div class="error">Error: ' + x.status +
149 ' ' + x.statusText + ' (' + x.responseText + ')</div>';
152 } else {
153 alert( 'bad target for sajax_do_call: not a function or object: ' + target );
157 sajax_debug( func_name + ' uri = ' + uri + ' / post = ' + post_data );
158 x.send( post_data );
159 sajax_debug( func_name + ' waiting..' );
160 delete x;
162 return true;
166 * @return boolean whether the browser supports XMLHttpRequest
168 window.wfSupportsAjax = function() {
169 var request = sajax_init_object();
170 var supportsAjax = request ? true : false;
171 delete request;
172 return supportsAjax;