The Callback Function

SWFObject 2.2 and greater includes a new callback feature that allows a function to be invoked upon completion of an embed, whether the embed was successful or not. This callback can be used with both dynamic and static publishing.

The callback function utilizes an event object as a parameter. This event object contains three properties:

  1. event.success: Boolean indicating success of the embed attempt
  2. event.id: The ID of the embedded <object> (string)
  3. event.ref: The <object> HTML element if the embed was successful, undefined if the embed failed.

Using the callback function with dynamic publishing

swfobject.embedSWF takes the callback function as a parameter (it is passed as the last parameter). Here's a typical example of a dynamic embed:

var flashvars = {};
var params = {};
var attributes = {};
 
swfobject.embedSWF("/path/to/sample.swf", "flashcontent", "550", "400", "7", "/path/to/expressInstall.swf", flashvars, params, attributes);

Here's the same code, but with a callback function that displays a message indicating the success status of the embed:

var flashvars = {};
var params = {};
var attributes = {};
var embedHandler = function (e){
 
   if(e.success){
      alert("The embed was successful!");
   } else {
      alert("The embed failed!");
   }
 
};
 
swfobject.embedSWF("/path/to/sample.swf", "flashcontent", "550", "400", "7", "/path/to/expressInstall.swf", flashvars, params, attributes, embedHandler);

Demo: Using the callback function with dynamic publishing

Using the callback function with static publishing

swfobject.registerObject takes the callback function as a parameter (it is passed as the last parameter). Here's the JavaScript portion of a typical static embed:

swfobject.registerObject("flashcontent", "9", "/path/to/expressinstall.swf");

Here's the same code, but with a callback function that displays a message if the SWF fails to embed:

var embedHandler = function (e){
 
   if(e.success){
      alert("The embed was successful!");
   } else {
      alert("The embed failed!");
   }
 
};
 
swfobject.registerObject("flashcontent", "9", "/path/to/expressinstall.swf", embedHandler);

Demo: Using the callback function with static publishing

Questions or Comments?

If you have questions or would like to point out an error, please post your remarks in the SWFObject Google Group. Thanks!