What is SWFObject 2.0?
- Offers two optimized Flash Player embed methods; a markup based approach and a method that relies on JavaScript
- Offers a JavaScript API that aims to provide a complete tool set for embedding SWF files and retrieving Flash Player related information [ http://code.google.com/p/swfobject/wiki/SWFObject_2_0_api_javascript_dev ]
- Utilizes only one small JavaScript file (size: 8.7Kb, GZIPed: 3.4Kb)
- Is the successor of SWFObject v1.5 [ http://blog.deconcept.com/swfobject/ ] and UFO [ http://www.bobbyvandersluis.com/ufo/ ]
- Will eventually replace the Adobe Flash Player Detection Kit [ http://www.adobe.com/products/flashplayer/download/detection_kit/ ]
- Intends to unify all existing Flash Player embed methods and provide a new standard for embedding Adobe Flash Player content
- Is an open source project by Geoff Stearns, Michael Williams, and Bobby van der Sluis, and is formerly known as SWFFix [ http://code.google.com/p/swffix/ ]
Why should you use SWFObject 2.0?
- Is more optimized and flexible than any other Flash Player embed method around
- Offers one solution for everybody: It shouldn't matter if you are an HTML, Flash, or JavaScript developer, there should be something in it for everyone
- Breaks the cycle of being locked into vendor specific markup and promotes the use of web standards and alternative content
- Uses unobtrusive JavaScript and JavaScript best practices
- Is easy to use
The A List Apart article Flash Embedding Cage Match [ http://www.alistapart.com/articles/flashembedcagematch/ ] describes the full rationale behind SWFObject 2.0.
Why does SWFObject 2.0 use JavaScript?
- Detects the Flash Player version and determines whether Flash content or alternative content should be shown, to avoid that outdated Flash plug-ins break Flash content
- Offers functionality to revert to alternative content in case of an outdated plug-in by means of a DOM manipulation (Note: if no Flash plug-in is installed the HTML object element automatically falls back to its nested alternative content)
- Offers the option to use Adobe Express Install to download the latest Flash Player
- Solves the issue that older Webkit engines ignore the object element's nested param elements by using the proprietary embed element instead (Note: this is the only place where proprietary markup is used and will be phased out in a future version)
- Offers the option to publish Flash Player content with JavaScript and hereby avoid possible click-to-activate mechanisms
- Offers an elaborate JavaScript API to perform common Flash Player and Flash content related tasks
Should I use the static or dynamic publishing method?
SWFObject 2.0 offers two distinct methods to embed Flash Player content:
- The static publishing method embeds both Flash content and alternative content using standards compliant markup, and uses JavaScript to resolve the issues that markup alone cannot solve
- The dynamic publishing method is based on marked up alternative content and uses JavaScript to replace this content with Flash content if the minimal Flash Player version is installed and enough JavaScript support is available (similar like previous versions of SWFObject and UFO)
The advantages of the static publishing method are:
- The actual authoring of standards compliant markup is promoted
- The mechanism of embedding Flash content does not rely on a scripting language, so your Flash content can reach a significant bigger audience:
- If you have the Flash plug-in installed, but have JavaScript disabled or a use a browser that doesn't support JavaScript, you will still be able to see your Flash content
- Flash will now also run on a device like Sony PSP, which has very poor JavaScript support
- Automated tools like RSS readers are able to pick up Flash content
The advantages of the dynamic publishing method are:
- It avoids click-to-activate mechanisms to activate active content in Internet Explorer 6/7 and Opera 9+. Please note that Microsoft is currently phasing out all active content from its Internet Explorer browsers [ http://www.swffix.org/devblog/?p=19 ]
- It integrates very well with scripted applications
How to embed Flash Player content using SWFObject 2.0 static publishing
STEP 1: Embed both Flash content and alternative content using standards compliant markup
SWFObject's base markup uses the nested-objects method (with proprietary Internet Explorer conditional comments) [ http://www.alistapart.com/articles/flashembedcagematch/ ] to ensure the most optimal cross-browser support by means of markup only, while being standards compliant and supporting alternative content [ http://www.swffix.org/testsuite/ ]:
<!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 v2.0 - step 1</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> </head> <body> <div> <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="780" height="420"> <param name="movie" value="myContent.swf" /> <!--[if !IE]>--> <object type="application/x-shockwave-flash" data="myContent.swf" width="780" height="420"> <!--<![endif]--> <p>Alternative content</p> <!--[if !IE]>--> </object> <!--<![endif]--> </object> </div> </body> </html>
NOTE: The nested-objects method requires a double object definition (the outer object targeting Internet Explorer and the inner object targeting all other browsers), so you need to define your object attributes and nested param elements twice.
Required attributes:
- classid (outer object element only, value is always clsid:D27CDB6E-AE6D-11cf-96B8-444553540000)
- type (inner object element only, value is always application/x-shockwave-flash)
- data (inner object element only, defines the URL of a SWF)
- width (both object elements, defines the width of a SWF)
- height (both object elements, defines the height of a SWF)
Required param element:
- movie (outer object element only, defines the URL of a SWF)
NOTE: We advise not to use the codebase attribute to point to the URL of the Flash plugin installer on Adobe's servers, because this is illegal according to the specifications which restrict its access to the domain of the current document only. We recommend the use of alternative content with a subtle message that a user can have a richer experience by downloading the Flash plugin instead.
How can you use HTML to configure your Flash content?
You can add the following often-used optional attributes [ http://www.w3schools.com/tags/tag_object.asp ] to the object element:
- id
- name
- class
- align
You can use the following optional Flash specific param elements [ http://www.adobe.com/cfusion/knowledgebase/index.cfm?id=tn_12701 ]:
- play
- loop
- menu
- quality
- scale
- salign
- wmode
- bgcolor
- base
- swliveconnect
- flashvars
- devicefont [ http://www.adobe.com/cfusion/knowledgebase/index.cfm?id=tn_13331 ]
- allowscriptaccess [ http://www.adobe.com/cfusion/knowledgebase/index.cfm?id=tn_16494 ] and [ http://www.adobe.com/go/kb402975 ]
- seamlesstabbing [ http://www.adobe.com/support/documentation/en/flashplayer/7/releasenotes.html ]
- allowfullscreen [ http://www.adobe.com/devnet/flashplayer/articles/full_screen_mode.html ]
- allownetworking [ http://livedocs.adobe.com/flash/9.0/main/00001079.html ]
Why should you use alternative content?
The object element allows you to nest alternative content inside of it, which will be displayed if Flash is not installed or supported. This content will also be picked up by search engines, making it a great tool for creating search-engine-friendly content. Summarizing, you should use alternative content when you like to create content that is accessible for people who browse the Web without plugins [ http://www.adobe.com/devnet/flash/articles/progressive_enhancement_03.html ], create search-engine-friendly content [ http://www.adobe.com/devnet/flash/articles/progressive_enhancement_04.html ] or tell visitors that they can have a richer user experience by downloading the Flash plug-in.
STEP 2: Include the SWFObject JavaScript library in the head of your HTML page
<!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 v2.0 - step 2</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <script type="text/javascript" src="swfobject.js"></script> </head> <body> <div> <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="780" height="420"> <param name="movie" value="myContent.swf" /> <!--[if !IE]>--> <object type="application/x-shockwave-flash" data="myContent.swf" width="780" height="420"> <!--<![endif]--> <p>Alternative content</p> <!--[if !IE]>--> </object> <!--<![endif]--> </object> </div> </body> </html>
STEP 3: Register your Flash content with the SWFObject library and tell SWFObject what to do with it
- The first argument (String, required) specifies the id used in the markup.
- The second argument (String, required) specifies the Flash player version your content is published for. It activates the Flash version detection for a SWF to determine whether to show Flash content or force alternative content by doing a DOM manipulation. 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".
- The third argument (String, optional) can be used to activate Adobe express install [ http://www.adobe.com/cfusion/knowledgebase/index.cfm?id=6a253b75 ] and specifies the URL of your express install SWF file. Express install displays a standardized Flash plugin download dialog instead of your Flash content when the required plugin version is not available. A default expressInstall.swf file is packaged with the project. It also contains the corresponding expressInstall.fla and AS files (in the SRC directory) to let you create your own custom express install experience. Please note that express install will only fire once (the first time that it is invoked), that it is only supported by Flash Player 6.0.65 or higher on Win or Mac platforms, and that it requires a minimal SWF size of 310x137px.
<!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 v2.0 - step 3</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"> swfobject.registerObject("myId", "9.0.0", "expressInstall.swf"); </script> </head> <body> <div> <object id="myId" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="780" height="420"> <param name="movie" value="myContent.swf" /> <!--[if !IE]>--> <object type="application/x-shockwave-flash" data="myContent.swf" width="780" height="420"> <!--<![endif]--> <p>Alternative content</p> <!--[if !IE]>--> </object> <!--<![endif]--> </object> </div> </body> </html>
TIPS
- Use the SWFObject 2.0 HTML and JavaScript generator to help you author your code [ http://code.google.com/p/swfobject/wiki/SWFObject_2_0_generator ]
- Just repeat steps 1 and 3 to embed multiple SWF files into one HTML page
- The easiest way to reference the active object element is by using the JavaScript API: swfobject.getObjectById(objectIdStr) [ http://code.google.com/p/swfobject/wiki/SWFObject_2_0_api_javascript_dev ]
How to embed Flash Player content using SWFObject 2.0 dynamic publishing
STEP 1: Create alternative content using standards compliant markup
SWFObject's dynamic embed method follows the principle of progressive enhancement [ http://www.adobe.com/devnet/flash/articles/progressive_enhancement.html ] and replaces alternative HTML content for Flash content when enough JavaScript and Flash plug-in support is available. First define your alternative content and label it with an id:
<!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 v2.0 dynamic embed - step 1</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> </head> <body> <div id="myContent"> <p>Alternative content</p> </div> </body> </html>
STEP 2: Include the SWFObject JavaScript library in the head of your HTML page
<!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 v2.0 dynamic embed - step 2</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <script type="text/javascript" src="swfobject.js"></script> </head> <body> <div id="myContent"> <p>Alternative content</p> </div> </body> </html>
STEP 3: Embed your SWF with JavaScript
- swfUrl (String, required) specifies the URL of your SWF
- id (String, required) specifies the id of the HTML element (containing your alternative content) you would like to have replaced by your Flash content
- width (String, required) specifies the width of your SWF
- height (String, required) specifies the height of your SWF
- version (String, required) specifies the Flash player version your SWF is published for (format is: "major.minor.release")
- expressInstallSwfurl (String, optional) specifies the URL of your express install SWF and activates Adobe express install [ http://www.adobe.com/cfusion/knowledgebase/index.cfm?id=6a253b75 ]. Please note that express install will only fire once (the first time that it is invoked), that it is only supported by Flash Player 6.0.65 or higher on Win or Mac platforms, and that it requires a minimal SWF size of 310x137px.
- flashvars (Object, optional) specifies your flashvars with name:value pairs
- params (Object, optional) specifies your nested object element params with name:value pairs
- attributes (Object, optional) specifies your object's attributes with name:value pairs
NOTE: You can omit the optional parameters, as long as you don't break the parameter order. If you don't want to use an optional parameter, but would like to use a following optional parameter, you can simply pass false as its value. For the flashvars, params and attributes JavaScript Objects, you can also pass an empty object instead: {}.
<!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 v2.0 dynamic embed - step 3</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"> swfobject.embedSWF("myContent.swf", "myContent", "300", "120", "9.0.0"); </script> </head> <body> <div id="myContent"> <p>Alternative content</p> </div> </body> </html>
How can you configure your Flash content?
You can add the following often-used optional attributes [ http://www.w3schools.com/tags/tag_object.asp ] to the object element:
- id (NOTE: when undefined, the object element automatically inherits the id from the alternative content container element)
- name
- styleclass (used instead of class, because this is also an ECMA4 reserved keyword)
- align
You can use the following optional Flash specific param elements [ http://www.adobe.com/cfusion/knowledgebase/index.cfm?id=tn_12701 ]:
- play
- loop
- menu
- quality
- scale
- salign
- wmode
- bgcolor
- base
- swliveconnect
- flashvars
- devicefont [ http://www.adobe.com/cfusion/knowledgebase/index.cfm?id=tn_13331 ]
- allowscriptaccess [ http://www.adobe.com/cfusion/knowledgebase/index.cfm?id=tn_16494 ] and [ http://www.adobe.com/go/kb402975 ]
- seamlesstabbing [ http://www.adobe.com/support/documentation/en/flashplayer/7/releasenotes.html ]
- allowfullscreen [ http://www.adobe.com/devnet/flashplayer/articles/full_screen_mode.html ]
- allownetworking [ http://livedocs.adobe.com/flash/9.0/main/00001079.html ]
How do you use JavaScript Objects to define your flashvars, params and object's attributes?
You can best define JavaScript Objects using the Object literal notation, which looks like:
<script type="text/javascript"> var flashvars = {}; var params = {}; var attributes = {}; swfobject.embedSWF("myContent.swf", "myContent", "300", "120", "9.0.0","expressInstall.swf", flashvars, params, attributes); </script>
<script type="text/javascript"> var flashvars = { name1: "hello", name2: "world", name3: "foobar" }; var params = { menu: "false" }; var attributes = { id: "myDynamicContent", name: "myDynamicContent" }; swfobject.embedSWF("myContent.swf", "myContent", "300", "120", "9.0.0","expressInstall.swf", flashvars, params, attributes); </script>
Or add properties and values after its creation by using a dot notation:
<script type="text/javascript"> var flashvars = {}; flashvars.name1 = "hello"; flashvars.name2 = "world"; flashvars.name3 = "foobar"; var params = {}; params.menu = "false"; var attributes = {}; attributes.id = "myDynamicContent"; attributes.name = "myDynamicContent"; swfobject.embedSWF("myContent.swf", "myContent", "300", "120", "9.0.0","expressInstall.swf", flashvars, params, attributes); </script>
<script type="text/javascript"> swfobject.embedSWF("myContent.swf", "myContent", "300", "120", "9.0.0","expressInstall.swf", {name1:"hello",name2:"world",name3:"foobar"}, {menu:"false"}, {id:"myDynamicContent",name:"myDynamicContent"}); </script>
If you don't want to use an argument you can define it as false or as an empty Object:
<script type="text/javascript"> var flashvars = false; var params = {}; var attributes = { id: "myDynamicContent", name: "myDynamicContent" }; swfobject.embedSWF("myContent.swf", "myContent", "300", "120", "9.0.0","expressInstall.swf", flashvars, params, attributes); </script>
<script type="text/javascript"> var flashvars = false; var params = { menu: "false", flashvars: "name1=hello&name2=world&name3=foobar" }; var attributes = { id: "myDynamicContent", name: "myDynamicContent" }; swfobject.embedSWF("myContent.swf", "myContent", "300", "120", "9.0.0","expressInstall.swf", flashvars, params, attributes); </script>
TIPS
- Use the SWFObject 2.0 HTML and JavaScript generator to help you author your code [ http://code.google.com/p/swfobject/wiki/SWFObject_2_0_generator ]
- Just repeat steps 1 and 3 to embed multiple SWF files into one HTML page
SWFObject 1.5 to SWFObject 2.0 migration tips
- SWFObject 2.0 is NOT backwards compatible with SWFObject 1.5
- It is now preferred to insert all script blocks in the head of your HTML page
- The library itself is now written in lowercase: swfobject instead of SWFObject
- Methods can only be accessed via the library (instead via a SWFObject instance in SWFObject 1.5)
- The API is entirely different and more elaborate: [ http://code.google.com/p/swfobject/wiki/SWFObject_2_0_api_javascript_dev ]
- SWFObject 2.0 replaces your entire alternative HTML content block, including the referenced HTML container element, for Flash content when enough JavaScript and Flash support is available, while SWFObject 1.5 only replaces the content inside the referenced HTML container. When you don't specify an id attribute, the object element will automatically inherit the id of the HTML container element of your alternative content.
UFO to SWFObject 2.0 migration tips
- SWFObject 2.0 replaces your entire alternative HTML content block, including the referenced HTML container element, for Flash content when enough JavaScript and Flash support is available, while UFO only replaces the content inside the referenced HTML container. When you don't specify an id attribute, the object element will automatically inherit the id of the HTML container element of your alternative content.
- UFO's setcontainercss feature has not been incorporated in SWFObject 2.0, however it can easily be replicated by using the SWFObject 2.0 API, see: swfobject.createCSS(selStr, declStr) in [ http://code.google.com/p/swfobject/wiki/SWFObject_2_0_api_javascript_dev ]
Does SWFObject 2.0 support MIME type application/xhtml+xml?
SWFObject does NOT support XML MIME types, which is a design decision.
There are a number of reasons why we are not supporting this:
- Only a very small (non-significant) amount of web authors is using it
- We are unsure if it is the direction to go. Internet Explorer does not support it and all other major browser vendors are aiming their arrows at a new standard way of HTML parsing (with HTML 5), which departs from the current W3C vision of parsing HTML as XML
- We save a considerate amount of file size and effort (testing, issue resolving) by not supporting it
COMMENTS POLICY
If you have any questions/comments about SWFObject 2.0 or have problems with an implementation:
- Please make sure you have read our FAQ [ http://code.google.com/p/swfobject/wiki/SWFObject_2_0_faq_web_authors ]
- Use the SWFObject 2.0 discussion group [ http://groups.google.com/group/swfobject ]
If you find any bugs or want to request a future enhancement, you can fill in an issue report on the SWFObject 2.0 issues page [ http://code.google.com/p/swfobject/issues/list ]