a4b713a378e1ee44ea615d4155c9437c9256496d
2 Copyright (c) 2003-2011, CKSource - Frederico Knabben. All rights reserved.
3 For licensing, see LICENSE.html or http://ckeditor.com/license
7 * @fileOverview Defines the {@link CKEDITOR.ajax} object, which holds ajax methods for
13 CKEDITOR
.plugins
.add( 'ajax',
19 * Ajax methods for data loading.
23 CKEDITOR
.ajax
= (function()
25 var createXMLHttpRequest = function()
27 // In IE, using the native XMLHttpRequest for local files may throw
28 // "Access is Denied" errors.
29 if ( !CKEDITOR
.env
.ie
|| location
.protocol
!= 'file:' )
30 try { return new XMLHttpRequest(); } catch(e
) {}
32 try { return new ActiveXObject( 'Msxml2.XMLHTTP' ); } catch (e
) {}
33 try { return new ActiveXObject( 'Microsoft.XMLHTTP' ); } catch (e
) {}
38 var checkStatus = function( xhr
)
43 // 0 : Returned when running locally (file://)
44 // 1223 : IE may change 204 to 1223 (see http://dev.jquery.com/ticket/1450)
46 return ( xhr
.readyState
== 4 &&
47 ( ( xhr
.status
>= 200 && xhr
.status
< 300 ) ||
50 xhr
.status
== 1223 ) );
53 var getResponseText = function( xhr
)
55 if ( checkStatus( xhr
) )
56 return xhr
.responseText
;
60 var getResponseXml = function( xhr
)
62 if ( checkStatus( xhr
) )
64 var xml
= xhr
.responseXML
;
65 return new CKEDITOR
.xml( xml
&& xml
.firstChild
? xml
: xhr
.responseText
);
70 var load = function( url
, callback
, getResponseFn
)
72 var async
= !!callback
;
74 var xhr
= createXMLHttpRequest();
79 xhr
.open( 'GET', url
, async
);
83 // TODO: perform leak checks on this closure.
85 xhr
.onreadystatechange = function()
87 if ( xhr
.readyState
== 4 )
89 callback( getResponseFn( xhr
) );
97 return async
? '' : getResponseFn( xhr
);
100 return /** @lends CKEDITOR.ajax */ {
103 * Loads data from an URL as plain text.
104 * @param {String} url The URL from which load data.
105 * @param {Function} [callback] A callback function to be called on
106 * data load. If not provided, the data will be loaded
108 * @returns {String} The loaded data. For asynchronous requests, an
109 * empty string. For invalid requests, null.
111 * // Load data synchronously.
112 * var data = CKEDITOR.ajax.load( 'somedata.txt' );
115 * // Load data asynchronously.
116 * var data = CKEDITOR.ajax.load( 'somedata.txt', function( data )
121 load : function( url
, callback
)
123 return load( url
, callback
, getResponseText
);
127 * Loads data from an URL as XML.
128 * @param {String} url The URL from which load data.
129 * @param {Function} [callback] A callback function to be called on
130 * data load. If not provided, the data will be loaded
132 * @returns {CKEDITOR.xml} An XML object holding the loaded data. For asynchronous requests, an
133 * empty string. For invalid requests, null.
135 * // Load XML synchronously.
136 * var xml = CKEDITOR.ajax.loadXml( 'somedata.xml' );
137 * alert( xml.getInnerXml( '//' ) );
139 * // Load XML asynchronously.
140 * var data = CKEDITOR.ajax.loadXml( 'somedata.xml', function( xml )
142 * alert( xml.getInnerXml( '//' ) );
145 loadXml : function( url
, callback
)
147 return load( url
, callback
, getResponseXml
);