My favorites | Sign in
Project Logo
                
Search
for
Updated Jun 11, 2009 by bobbyvandersluis
Labels: Featured
api  
SWFObject JavaScript API documentation

SWFObject JavaScript API documentation

SWFObject 2 contains an API that allows JavaScript developers to reuse SWFObject's internal functions and aims to deliver a complete tool set for publishing SWF's and retrieving Flash player related information.

Methods

swfobject.registerObject(objectIdStr, swfVersionStr, xiSwfUrlStr, callbackFn)

Embed Flash content and alternative content using standards compliant markup (the nested-objects method with proprietary Internet Explorer conditional comments), and use JavaScript to resolve the issues that markup alone cannot solve (also called static publishing).

Arguments:
Where callbackFn is a JavaScript function that has an event object as a parameter:

function callbackFn(e) { ... }
Properties of this event object are:
  • success, Boolean to indicate whether the embedding of a SWF was success or not
  • id, String indicating the ID used in swfobject.registerObject
  • ref, HTML object element reference (returns undefined when success=false)

This method is explained in "How to embed Flash Player content using SWFObject static publishing" of the SWFObject documentation page.

Basic static publishing example

Basic static publishing example with callback

swfobject.getObjectById(objectIdStr)

NOTE: For static publishing only (or: when using swfobject.registerObject() only)
Returns the active object element. One of the side-effects of the nested-objects approach is that per SWF there are two object elements available in the HTML code, however you can only use one id or name attribute, because these have to be unique within a web page.
The following browsers map to the following active object element:
You can reference the active object element by:
  1. Add an id to the outer object element
  2. Use the swfobject.getObjectById() method to reference the active object element
An example:
var obj = swfobject.getObjectById("myId");
if (obj) {
  obj.doSomething(); // e.g. an external interface call
}
Sample page

swfobject.embedSWF(swfUrlStr, replaceElemIdStr, widthStr, heightStr, swfVersionStr, xiSwfUrlStr, flashvarsObj, parObj, attObj, callbackFn)

Insert alternative content using standards compliant markup and embed Flash content with unobtrusive JavaScript (also called dynamic publishing).

Arguments:
Where callbackFn is a JavaScript function that has an event object as a parameter:

function callbackFn(e) { ... }
Properties of this event object are:
  • success, Boolean to indicate whether the embedding of a SWF was success or not
  • id, String indicating the ID used in swfobject.registerObject
  • ref, HTML object element reference (returns undefined when success=false)

This method is explained in "How to embed Flash Player content using SWFObject dynamic publishing" of the SWFObject documentation page
Basic dynamic publishing example
Basic dynamic publishing example with callback

swfobject.getFlashPlayerVersion()

Returns a JavaScript object containing the major version (major:Number), minor version (minor:Number) and release version (release:Number) of an installed Flash player. Please note that while Flash version numbers normally consist of major.minor.release.build, SWFObject only looks at the first 3 numbers, so both "WIN 9,0,18,0" (IE) or "Shockwave Flash 9 r18" (all other browsers) will translate to "9.0.18".
An example:
var playerVersion = swfobject.getFlashPlayerVersion(); // returns a JavaScript object
var majorVersion = playerVersion.major; // access the major, minor and release version numbers via their respective properties
Sample page

swfobject.hasFlashPlayerVersion(versionStr)

Returns a Boolean to indicate whether or not a specific minimum version of the Flash plugin is installed. Please note that while Flash version numbers normally consist of major.minor.release.build, SWFObject only looks at the first 3 numbers, so both "WIN 9,0,18,0" (IE) or "Shockwave Flash 9 r18" (all other browsers) will translate to "9.0.18".
An example:
if (swfobject.hasFlashPlayerVersion("9.0.18")) {
  // has Flash
}
else {
  // no Flash
}
Sample page

swfobject.addLoadEvent(fn)

A safe (it doesn't overwrite existing onload events) cross-browser method to execute a function on the window onload event (which will fire as soon as a web page including all of its assets are loaded). Based on a solution by James Edwards.
An example:
function sayHi() {
  alert("Hi!");
}
swfobject.addLoadEvent(sayHi);
Sample page

swfobject.addDomLoadEvent(fn)

A cross-browser method to execute a function as soon as the DOM of a web page is available. This method is supported by Gecko based browsers - like Firefox -, IE, Opera9+, and, Safari. For all other browsers the method will fall back to the addLoadEvent method. Based on a solution by Dean Edwards.
An example:
function sayHi() {
  alert("Hi!");
}
swfobject.addDomLoadEvent(sayHi);
Sample page

swfobject.createSWF(attObj, parObj, replaceElemIdStr)

Exposes SWFObject's internal cross-browser method to create a SWF. This 'low level' method will primarily be useful when combining it with other JavaScript libraries:

Returns the newly created object element
Note: you should only call this function when the DOM of a web page is available

Don't specify the following attributes:
  • classid:"D27CDB6E-AE6D-11cf-96B8-444553540000" (SWFObject publishes this automatically for Internet Explorer on Windows)
  • type:"application/x-shockwave-flash" (SWFObject publishes this automatically for all browsers except Internet Explorer on Windows)
  • codebase:"http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0" (deprecated)

Don't specify the following param element:
  • movie (use the object element's data attribute instead, SWFObject publishes this automatically for Internet Explorer on Windows)

A basic example:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
  <head>
    <title>SWFObject - low level dynamic publishing example</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <script type="text/javascript" src="swfobject.js"></script>
    <script type="text/javascript">
    if (swfobject.hasFlashPlayerVersion("6.0.0")) {
      var fn = function() {
        var att = { data:"test.swf", width:"780", height:"400" };
        var par = { flashvars:"foo=bar" };
        var id = "replaceMe";
        var myObject = swfobject.createSWF(att, par, id);
      };
      swfobject.addDomLoadEvent(fn);
    }
    </script>
  </head>
  <body>
    <div id="replaceMe">Alternative content</div>
  </body>
</html>
Sample page

Sample page that uses the returned object element for browser communication

swfobject.removeSWF(objElemIdStr)

NOTE: This method has been added in SWFObject 2.1 and is therefore only available from SWFObject 2.1 onwards
Removes a SWF from your web page. Is especially built to safely (only remove a SWF after it has been loaded, to avoid broken references) and completely (nullify references to avoid memory leaks) remove a SWF in Internet Explorer.
An example:
swfobject.removeSWF("myContent");
Sample page

swfobject.createCSS(selStr, declStr, mediaStr, newStyleBoolean)

Exposes SWFObject's internal cross-browser method to create a dynamic stylesheet. It's most important feature is that it can be called before the DOM of a page is loaded. Based on a solution by Bobby van der Sluis:
  • selStr (String, required) represents a CSS selector
  • declStr (String, required) represents a CSS declaration
  • mediaStr (String, optional) to indicate the CSS media type (media type scree is the default, SWFObject 2.2+)
  • newStyleBoolean (Boolean, optional) to indicate whether a new dynamic style element should be appended to the head of the HTML document, or that SWFObject should reuse a possible earlier created dynamic style element (default, SWFObject 2.2+)
From SWFObject 2.2 onwards this method will by default append new dynamic styles to an earlier created dynamic style sheet (if available).

An example:

if (swfobject.hasFlashPlayerVersion("6.0.0")) {
  // Overwrite regular CSS used for alternative content to enable Full Browser Flash
  swfobject.createCSS("html", "height:100%;");
  swfobject.createCSS("body", "margin:0; padding:0; overflow:hidden; height:100%;");
  swfobject.createCSS("#container", "height:100%;");
}
Basic example page

Advanced example page

swfobject.getQueryParamValue(paramStr)

Utility function that returns the value of the paramStr parameter in the URL query string or hash string. Similar functionality was already available in SWFObject 1.5 and lower.
To secure the method from possible XSS abuse SWFObject 2.1 will encode the returned value - using the JavaScript encodeURIComponent() function - if the query string includes one of the following characters:
[ \ " < > . ; ]
An example that uses the fictive URL "http://www.yoururl.com?foo=bar&abc=123" and passes the parameters from the URL query string to the SWF via flashvars:
var flashvars = {};
if (swfobject.getQueryParamValue("foo") && swfobject.getQueryParamValue("abc")) {
  flashvars.foo = swfobject.getQueryParamValue("foo");
  flashvars.abc = swfobject.getQueryParamValue("abc");
}
var params = {};
var attributes = {};
swfobject.embedSWF("myContent.swf", "altContent", "100%", "100%", "9.0.0","expressInstall.swf", flashvars, params, attributes);
Example page

swfobject.switchOffAutoHideShow()

Disable SWFObject's default show/hide behavior (SWFObject 2.2+).
Ensure to call swfobject.switchOffAutoHideShow() after you have included the SWFObject library, but before any swfobject.registerObject() or swfobject.embedSWF() calls, like:
<script type="text/javascript" src="swfobject.js"></script>
<script type="text/javascript">
swfobject.switchOffAutoHideShow();
swfobject.embedSWF("test.swf", "myContent", "300", "120", "9", "expressInstall.swf");
</script>
Basic static publishing example page

Basic dynamic publishing example page

swfobject.showExpressInstall(att, par, replaceElemIdStr, callbackFn)

Enables developers to reuse SWFObject's internal methods to create their custom Express Install and activate it via the API (SWFObject 2.2+):
  • attObj is a JavaScript Object that contains the name value pairs of the object element's attributes and values
  • parObj is a JavaScript Object that contains the name value pairs of the object element's nested param element's names and values
  • replaceElemIdStr is the id attribute of the HTML element that you want to have replaced by your SWF content
  • callbackFn (JavaScript function, optional) can be used to define a callback function that is called on both success or failure of embedding a SWF file (SWFObject 2.2+)

Where callbackFn is a JavaScript function that has an event object as a parameter:

function callbackFn(e) { ... }
Properties of this event object are:
  • success, Boolean to indicate whether the embedding of a SWF was success or not
  • id, String indicating the ID used in swfobject.registerObject
  • ref, HTML object element reference (returns undefined when success=false)
An example:
<script type="text/javascript" src="swfobject.js"></script>
<script type="text/javascript">
function cancelFunction() {
        alert("Express Install was cancelled");
}
if (swfobject.hasFlashPlayerVersion("10")) {
        var fn = function() {
                var att = { data:"flashContent.swf", width:"300", height:"120" };
                var par = {};
                var id = "myContent";
                swfobject.createSWF(att, par, id);
        };
        
}
else {
        var fn = function() {
                var att = { data:"expressInstall.swf", width:"600", height:"240" };
                var par = {};
                var id = "myContent";
                swfobject.showExpressInstall(att, par, id, cancelFunction);
        }
}
swfobject.addDomLoadEvent(fn);
</script>
Example page

Properties

SWFObject 2.2+ exposes the properties of its internal user agent detection for library authors (e.g. developers that like to write extensions for SWFObject).
An example:
<script type="text/javascript" src="swfobject.js"></script>
<script type="text/javascript">
function test() {
    var output = "swfobject.ua.w3 = " + swfobject.ua.w3;
    output += "\nswfobject.ua.pv[0] (major version) = " + swfobject.ua.pv[0];
    output += "\nswfobject.ua.pv[1] (minor version) = " + swfobject.ua.pv[1];
    output += "\nswfobject.ua.pv[2] (release version) = " + swfobject.ua.pv[2];
    output += "\nswfobject.ua.wk = " + swfobject.ua.wk;
    output += "\nswfobject.ua.ie = " + swfobject.ua.ie;
    output += "\nswfobject.ua.win = " + swfobject.ua.win;
    output += "\nswfobject.ua.mac = " + swfobject.ua.mac;
    alert(output);
}
swfobject.addLoadEvent(test);
</script>
Example page

swfobject.ua.w3

returns a Boolean whether or not W3C DOM methods are supported

swfobject.ua.pv

returns an Array that contains the major, minor and release version number of the Flash Player

swfobject.ua.wk

returns the Webkit version or false if not Webkit

swfobject.ua.ie

returns a Boolean to indicate whether a visitor uses Internet Explorer on Windows or not

swfobject.ua.win

returns a Boolean to indicate whether a visitor uses Windows OS or not

swfobject.ua.mac

returns a Boolean to indicate whether a visitor uses Mac OS or not

SWF native methods and properties

SWF content also includes native methods and properties that can be accessed via JavaScript:

Comments

The comments functionality on this wiki has been switched off to avoid spam and abuse.

If you have any questions/comments about SWFObject or its documentation, or have problems with an implementation:

  1. Please make sure you have read our FAQ
  2. Use the SWFObject discussion group

If you find any bugs or want to request a future enhancement, you can fill in an issue report on the SWFObject issues page


Hosted by Google Code