Preload assets with JavaScript, load-complete callback for single assets include SWF/Flash

Currently i need to know within JavaScript when a SWF-file was loaded completly. You could easily handle this with a single ExternalInterface call from within the Flash-application. This is not a very nice solution if this has to be done for each of you flash-applications. This is the neccesary way if you also have to access the flash-application from javascript-side. But in my case i just need to know when the flash is loaded and available in the browser cache.

For this i wrote the swfJSPreLoader. This preloader accepts an array of assets (jpg,gif,png,swf) you would like to preload. Internally it will create an flash-application which does the preloading. You could define several callback handlers. So JavaScript gets to know when all assets are loaded or each single asset is loaded including SWF-files.

<script type="text/javascript">
	var swfJSPreLoaderConfig = {
		'_swfPath':'./', // configure path to preloader SWF file
		'assets':[	'assets/teaser.swf',
		'assetLoaded': function( asset, bytes, status ){
			alert( asset + ' loaded (' + bytes + ')' );
		'loadComplete': function(){
			alert('all assets loaded. totally loaded bytes: ' + swfJSPreLoader.loadedBytes() );
		'loadError': function(){
			alert('load error');
		'callback': function(){
			swfJSPreLoader.addAsset('assets/teaser2.swf',function( status ){
															alert('teaser2 loaded status:' + status );
<script type="text/javascript" src="swfobject.js"></script>
<script type="text/javascript" src="jquery-1.3.2.min.js"></script>
<script type="text/javascript" src="swfJSPreLoader.js"></script>

To use the swfJSPreLoader you have to include three js files for swfObjects, JQuery and the swfJSPreLoader.js. You also have to define an JS variable swfJSPreLoaderConfig to configure the preloader. The config-variable could hold the following items:

  • assets: assets is an array of the assets which should be loaded. The asset-paths must be relative the html page. If you insert wrong asset pathes you could define an error handler. The assets array is optional. You could also add assets via method call.
  • assetLoaded: assetLoaded defines the callback method for each loaded asset. So this method will be called each time an asset is loaded completly. There are three parameters. asset – gives the asset-path; bytes – gives the number of loaded bytes of the current asset; status – gives the loading status ( 200: Ok, 404: Loading error )
  • loadComplete:The loadComplete callback method will be called after the queue of assets to load is empty. Not all assets need be loaded successfully. If not the error-callback will be fired.
  • loadError:The loadError callback will be triggered if the current asset could not be loaded correctly.
  • callback:With callback you could register a method which is called after the initialization of the swfJSPreLoader. After this method is called you could use the public methods of the pre loader.

There are two public methods of the swfJSPreLoader. Both methods are ready after the callback method is called and the initialisation is ready ( the loader flash is instanziated ).

swfJSPreLoader.addAsset: The addAsset method allows you to add a single asset to add to the loading queue. It takes to parameters. The first is the path to the asset, the second is the callback handler, which will be fired after this specific asset was loaded. The callback will also be called if the loading fails. The callback gets one argument status with the loading status ( 200: Ok, 404: Loading error ).

 * @public
 * adds asset to loading queue
 * @param {String} assetPath - path to assets to load
 * @param {Function} callback - optional callback handler when loading is complete
addAsset:function( assetPath, callback )
						function( status ){
							alert('teaser2 loaded status:' + status );

swfJSPreLoader.loadedBytes: The loadedBytes method returns the number of total loaded bytes currently loaded bytes on each call.

Update 2011-08-07:swfJSPreLoader Flash Builder Project Sourcefiles (ZIP, 62kb)

Update 2010-05-17:swfJSPreLoader-Test (ZIP, 532kb)

Examples (added 2010-11-07, updated 2011-08-07)

After this seems to be difficult here are some more working examples. All tested at FF4.0b6/OSX; Safari5/OSX; Chrome7.0/OSX; FF3.6/WinXP; IE8/WinXP. Please not that this should be tested from an Server environment and not from local filesystem and requires an installed Flash Plugin.

Added an example for loading from other servers via crossdomain policy file and showing percent loading status.

download examples

I hope this is helpfull for anybody.