2 Copyright (c) 2003-2009, CKSource - Frederico Knabben. All rights reserved.
\r
3 For licensing, see LICENSE.html or http://ckeditor.com/license
\r
7 * @fileOverview Defines the {@link CKEDITOR.scriptLoader} object, used to load scripts
\r
12 * Load scripts asynchronously.
\r
16 CKEDITOR.scriptLoader = (function()
\r
18 var uniqueScripts = {};
\r
19 var waitingList = {};
\r
21 return /** @lends CKEDITOR.scriptLoader */ {
\r
23 * Loads one or more external script checking if not already loaded
\r
24 * previously by this function.
\r
25 * @param {String|Array} scriptUrl One or more URLs pointing to the
\r
26 * scripts to be loaded.
\r
27 * @param {Function} [callback] A function to be called when the script
\r
28 * is loaded and executed. If a string is passed to "scriptUrl", a
\r
29 * boolean parameter is passed to the callback, indicating the
\r
30 * success of the load. If an array is passed instead, two array
\r
31 * parameters are passed to the callback; the first contains the
\r
32 * URLs that have been properly loaded, and the second the failed
\r
34 * @param {Object} [scope] The scope ("this" reference) to be used for
\r
35 * the callback call. Default to {@link CKEDITOR}.
\r
36 * @param {Boolean} [noCheck] Indicates that the script must be loaded
\r
37 * anyway, not checking if it has already loaded.
\r
39 * CKEDITOR.scriptLoader.load( '/myscript.js' );
\r
41 * CKEDITOR.scriptLoader.load( '/myscript.js', function( success )
\r
43 * // Alerts "true" if the script has been properly loaded.
\r
44 * // HTTP error 404 should return "false".
\r
48 * CKEDITOR.scriptLoader.load( [ '/myscript1.js', '/myscript2.js' ], function( completed, failed )
\r
50 * alert( 'Number of scripts loaded: ' + completed.length );
\r
51 * alert( 'Number of failures: ' + failed.length );
\r
54 load : function( scriptUrl, callback, scope, noCheck )
\r
56 var isString = ( typeof scriptUrl == 'string' );
\r
59 scriptUrl = [ scriptUrl ];
\r
64 var scriptCount = scriptUrl.length,
\r
68 var doCallback = function( success )
\r
73 callback.call( scope, success );
\r
75 callback.call( scope, completed, failed );
\r
79 if ( scriptCount === 0 )
\r
85 var checkLoaded = function( url, success )
\r
87 ( success ? completed : failed ).push( url );
\r
89 if ( --scriptCount <= 0 )
\r
90 doCallback( success );
\r
93 var onLoad = function( url, success )
\r
95 // Mark this script as loaded.
\r
96 uniqueScripts[ url ] = 1;
\r
98 // Get the list of callback checks waiting for this file.
\r
99 var waitingInfo = waitingList[ url ];
\r
100 delete waitingList[ url ];
\r
102 // Check all callbacks waiting for this file.
\r
103 for ( var i = 0 ; i < waitingInfo.length ; i++ )
\r
104 waitingInfo[ i ]( url, success );
\r
107 var loadScript = function( url )
\r
109 if ( noCheck !== true && uniqueScripts[ url ] )
\r
111 checkLoaded( url, true );
\r
115 var waitingInfo = waitingList[ url ] || ( waitingList[ url ] = [] );
\r
116 waitingInfo.push( checkLoaded );
\r
118 // Load it only for the first request.
\r
119 if ( waitingInfo.length > 1 )
\r
122 // Create the <script> element.
\r
123 var script = new CKEDITOR.dom.element( 'script' );
\r
124 script.setAttributes( {
\r
125 type : 'text/javascript',
\r
130 if ( CKEDITOR.env.ie )
\r
132 // FIXME: For IE, we are not able to return false on error (like 404).
\r
135 script.$.onreadystatechange = function ()
\r
137 if ( script.$.readyState == 'loaded' || script.$.readyState == 'complete' )
\r
139 script.$.onreadystatechange = null;
\r
140 onLoad( url, true );
\r
147 script.$.onload = function()
\r
149 // Some browsers, such as Safari, may call the onLoad function
\r
150 // immediately. Which will break the loading sequence. (#3661)
\r
151 setTimeout( function() { onLoad( url, true ); }, 0 );
\r
154 // FIXME: Opera and Safari will not fire onerror.
\r
157 script.$.onerror = function()
\r
159 onLoad( url, false );
\r
164 // Append it to <head>.
\r
165 script.appendTo( CKEDITOR.document.getHead() );
\r
167 CKEDITOR.fire( 'download', url ); // @Packager.RemoveLine
\r
170 for ( var i = 0 ; i < scriptCount ; i++ )
\r
172 loadScript( scriptUrl[ i ] );
\r
177 * Executes a JavaScript code into the current document.
\r
178 * @param {String} code The code to be executed.
\r
180 * CKEDITOR.scriptLoader.loadCode( 'var x = 10;' );
\r
181 * alert( x ); // "10"
\r
183 loadCode : function( code )
\r
185 // Create the <script> element.
\r
186 var script = new CKEDITOR.dom.element( 'script' );
\r
187 script.setAttribute( 'type', 'text/javascript' );
\r
188 script.appendText( code );
\r
190 // Append it to <head>.
\r
191 script.appendTo( CKEDITOR.document.getHead() );
\r