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. See Flash Embedding Cage Match) to ensure the most optimal cross-browser support by means of markup only, while being standards compliant and supporting alternative content (See Flash Embed Test Suite):<!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 - 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)
- movie (outer object element only, defines the URL of a SWF)
How can you use HTML to configure your Flash content?
You can add the following often-used optional attributes to the object element:- id
- name
- class
- align
- play
- loop
- menu
- quality
- scale
- salign
- wmode
- bgcolor
- base
- swliveconnect
- flashvars
- devicefont (more info)
- allowscriptaccess (more info here and here)
- seamlesstabbing (more info)
- allowfullscreen (more info)
- allownetworking (more info)
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, create search-engine-friendly content 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
The SWFObject library consists of one external JavaScript file. SWFObject will be executed as soon as it is read and will perform all DOM manipulations as soon as the DOM is loaded - for all browsers that support this, like IE, Firefox, Safari and Opera 9+ - or otherwise as soon as the onload event fires:<!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 - 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
First add a unique id to the outer object tag that defines your Flash content. Second add the swfobject.registerObject method:- 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". If you only want to test for a major version you can omit the minor and release numbers, like "9" instead of "9.0.0".
- The third argument (String, optional) can be used to activate Adobe express install 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.
- The fourth argument (JavaScript function, optional) can be used to define a callback function that is called on both success or failure of creating a Flash plug-in <object> on the page (see API documentation)
<!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 - 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.115", "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 HTML and JavaScript generator to help you author your code
- 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)
How to embed Flash Player content using SWFObject dynamic publishing
STEP 1: Create alternative content using standards compliant markup
SWFObject's dynamic embed method follows the principle of progressive enhancement 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 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
The SWFObject library consists of one external JavaScript file. SWFObject will be executed as soon as it is read and will perform all DOM manipulations as soon as the DOM is loaded - for all browsers that support this, like IE, Firefox, Safari and Opera 9+ - or otherwise as soon as the onload event fires:<!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 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
swfobject.embedSWF(swfUrl, id, width, height, version, expressInstallSwfurl, flashvars, params, attributes, callbackFn) has five required and five optional arguments:- 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" or "major")
- expressInstallSwfurl (String, optional) specifies the URL of your express install SWF and activates Adobe express install. 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
- callbackFn (JavaScript function, optional) can be used to define a callback function that is called on both success or failure of creating a Flash plug-in <object> on the page (see API documentation)
<!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 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 to the object element:- id (NOTE: when undefined, the object element automatically inherits the id from the alternative content container element)
- align
- name
- styleclass (see note about class)
- class
Example:
var attributes = { id: "myId", align: "left", styleclass: "myclass" };If you would rather use class instead of styleclass, wrap the word class in quotes like this:
var attributes = { id: "myId", align: "left", "class": "myclass" };You can use the following optional Flash specific param elements (more info):
- play
- loop
- menu
- quality
- scale
- salign
- wmode
- bgcolor
- base
- swliveconnect
- flashvars
- devicefont (more info)
- allowscriptaccess (more info here and here)
- seamlesstabbing (more info)
- allowfullscreen (more info)
- allownetworking (more info)
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>You can add your name:value pairs while you define an object (note: please make sure not to put a comma behind the last name:value pair inside an object)
<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>Which can also be written as (the less readable shorthand version for the die-hard scripter who love one-liners):
<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 optional argument you can define it as false or as an empty Object (NOTE: from SWFObject 2.1 onwards you can also use null or 0):
<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>The flashvars Object is a shorthand notation that is there for your ease of use. You could potentially ignore it and specify your flashvars via the params Object:
<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>
Flash is a good tool for the webpage, but is hard to change if there is a modifid in the text or picture. in the future, HTML5 is the trendency.
ReplyDelete