1 /*
  2  * CKEditor - The text editor for Internet - http://ckeditor.com
  3  * Copyright (C) 2003-2008 Frederico Caldeira Knabben
  4  *
  5  * == BEGIN LICENSE ==
  6  *
  7  * Licensed under the terms of any of the following licenses at your
  8  * choice:
  9  *
 10  *  - GNU General Public License Version 2 or later (the "GPL")
 11  *    http://www.gnu.org/licenses/gpl.html
 12  *
 13  *  - GNU Lesser General Public License Version 2.1 or later (the "LGPL")
 14  *    http://www.gnu.org/licenses/lgpl.html
 15  *
 16  *  - Mozilla Public License Version 1.1 or later (the "MPL")
 17  *    http://www.mozilla.org/MPL/MPL-1.1.html
 18  *
 19  * == END LICENSE ==
 20  */
 21 
 22 /**
 23  * Load scripts asynchronously.
 24  * @namespace
 25  * @example
 26  */
 27 CKEDITOR.scriptLoader = (function()
 28 {
 29 	var uniqueScripts = {};
 30 
 31 	return /** @lends CKEDITOR.scriptLoader */ {
 32 		load : function( scriptUrl, callback, scope, noCheck )
 33 		{
 34 			if ( noCheck !== true )
 35 			{
 36 				if ( uniqueScripts[ scriptUrl ] )
 37 					return false;
 38 
 39 				uniqueScripts[ scriptUrl ] = true;
 40 			}
 41 
 42 			// Create the <script> element.
 43 			var script = new CKEDITOR.dom.element( 'script' );
 44 			script.setAttributes( {
 45 				type : 'text/javascript',
 46 				src : scriptUrl } );
 47 
 48 			if ( callback )
 49 			{
 50 				if ( CKEDITOR.env.ie )
 51 				{
 52 					// FIXME: For IE, we are not able to return false on error (like 404).
 53 
 54 					script.$.onreadystatechange = function ()
 55 					{
 56 						if ( script.$.readyState == 'loaded' || script.$.readyState == 'complete' )
 57 						{
 58 							script.$.onreadystatechange = null;
 59 							callback.call( scope || CKEDITOR, true );
 60 						}
 61 					}
 62 				}
 63 				else
 64 				{
 65 					script.$.onload = function()
 66 					{
 67 						callback.call( scope || CKEDITOR, true );
 68 					}
 69 
 70 					// FIXME: Opera and Safari will not fire onerror.
 71 
 72 					script.$.onerror = function()
 73 					{
 74 						callback.call( scope || CKEDITOR, false );
 75 					}
 76 				}
 77 			}
 78 
 79 			// Append it to <head>.
 80 			script.appendTo( CKEDITOR.dom.element.getHead() );
 81 
 82 			return true;
 83 		},
 84 
 85 		loadCode : function( code )
 86 		{
 87 			// Create the <script> element.
 88 			var script = new CKEDITOR.dom.element( 'script' );
 89 			script.setAttribute( 'type', 'text/javascript' );
 90 
 91 			if ( CKEDITOR.env.ie )
 92 				script.setText( code );
 93 			else
 94 				script.appendText( code );
 95 
 96 			// Append it to <head>.
 97 			script.appendTo( CKEDITOR.dom.element.getHead() );
 98 		}
 99 	};
100 })();
101