RSS

SWFObject and Best Practice Implementation

Mon, Jun 29, 2009

Flash, Flex, Latest, Tips, Tutorials

SWFObject and Best Practice Implementation

With more and more full screen Flash web sites appearing on the internet, it’s important to understand how to properly embed the Flash movie in the browser window.

In this tutorial we will cover best practice embedding of SWF movies using SWFObject with a custom function for adding browser scrollbars for smaller browser windows.

Requirements

Download Source Files

Pre-Requisites

Basic knowledge of Flash and HTML. This tutorial is intended for all skill levels.

Step 1 Setup your SWF for full screen viewing.

Create a new SWF movie, this can be any size you want, for this example I’ve setup a new movie at 600 pixels wide by 400 pixels high ( 600 x 400 px).

When you embed an SWF in your HTML, it’s default behavior is to scale to fill the width and height of the browser window. This means that if you create a SWF at 600×400pixels and view it on a browser sized at 1024×768 your beautifully designed 600×400px SWF will scale to 1024×768 and look .

For the purposes of this tutorial, we want our SWF to stay at scaled 100%, if our 600×400px design is viewed on a larger browser window such as 1024×768 px, we want the Flash stage to be centered and for the extra space around the stage to be filled with our overflow background.

To stop our SWF from scaling, we simply add this line to Frame 1 of our ActionScript:

ActionScript 2:

Stage.scaleMode = "noScale";

ActionScript 3:

stage.scaleMode = StageScaleMode.NO_SCALE;

Step 2 Setup your xHTML Template

This is a standard W3C Compliant xHTML Template that I recommend you use a basis for all of your sites.

<!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>Title</title>
	</head>
	<body>
	</body>
</html>

Save this file as test.html

Step 3 Add SWFObject Dynamic SWF embedding

Extract the SWFObject zip file you downloaded earlier and copy the SWFObject.js and expressInstall.swf files into the same directory as your new index.html file.

Step 3a

Add the SWFObject.js include to the head tag of our HTML template.

<script type="text/javascript" src="SWFObject.js"></script>
<script type="text/javascript">
//<![CDATA[
SWFObject.embedSWF("example.swf", "flashMovie", "100%", "100%", "6.0.0","expressInstall.swf");
//]]>
</script>

Step 3b

Add the flashMovie div and flashContainer div to the body tag of our HTML Template.

<div id="flashContainer">
	<div id="flashMovie">
		<h1>Alternative flashMovie</h1>
		<p><a href="http://www.adobe.com/go/getflashplayer"><img src="http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif" alt="Get Adobe Flash player" /></a></p>
	</div>
</div>

Step 3c

Add the following CSS to style the div flashContainer and flashMovie in the head tag. This resets the browser margins and padding to 0px, ensuring the SWF fille the entire browser window and sets the flashContainer div to fill to 100% of the browsers width and height.

	<style type="text/css" media="screen">
		html, body{
			margin:0;
			padding:0;
			height:100%;
		}
		#flashContainer{
			width:100%;
			height:100%;
		}
	</style>

Also add the following meta under your title tag.

<meta http-equiv="flashMovie-Type" flashMovie="text/html; charset=iso-8859-1" />

So our HTML template should now look like:

<!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>Flash SWF Embedding, the right way min-width, min-height and scroll bars</title>
		<meta http-equiv="flashMovie-Type" flashMovie="text/html; charset=iso-8859-1" />
		<style type="text/css" media="screen">
			html, body{
				margin:0;
				padding:0;
				height:100%;
			}
			#flashContainer{
				width:100%;
				height:100%;
			}
		</style>
		<script type="text/javascript" src="SWFObject.js"></script>
		<script type="text/javascript">
		//<![CDATA[
		SWFObject.embedSWF("example.swf", "flashMovie", "100%", "100%", "6.0.0","expressInstall.swf");
		//]]>
		</script>
	</head>
	<body>
		<div id="flashContainer">
			<div id="flashMovie">
				<h1>Alternative flashMovie</h1>
				<p><a href="http://www.adobe.com/go/getflashplayer"><img src="http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif" alt="Get Adobe Flash player" /></a></p>
			</div>
		</div>
	</body>
</html>

The important line for embedding your own Flash SWF is here:

SWFObject.embedSWF("example.swf", "flashMovie", "100%", "100%", "6.0.0","expressInstall.swf");

You can change example.swf to the name of your movie. You can also include paths, so if youe swf was located in ./test/ and was name test_swf.swf your embed tag would look like:

SWFObject.embedSWF("./test/test_swf.swf", "flashMovie", "100%", "100%", "6.0.0","expressInstall.swf");

The 4th parameter, 6.0.0 sets the minimum Flash player version. For example, ff you’re publishing to Flash Player 8, set this variable to 8.0.0

The 5th parameter, “expressInstall.swf”, tells SWFObject to automatically upgrade older Flash players to the required version. Make sure the expressInstall.swf is uploaded in the directory of your HTML.

More information on embedding with SWFObject is available at http://code.google.com/p/SWFObject/w/list

Step 4 Adding custom JavaScript to get the browser viewport size

Now we want to add a JavaScript helper function to set the minimum size of our Flash movie, so that if the browser is re sized below the minimum width or height scroll bars will be added. This replicates the functionality of a standard HTML web site.

Here is the helper function:

		function setFlashSize(flashWidth,flashHeight){
			var size = [0,0];
			if( typeof( window.innerWidth ) == 'number' ) {
				size = [window.innerWidth, window.innerHeight];
			} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
				size = [document.documentElement.clientWidth, document.documentElement.clientHeight];
			} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
				size = [document.body.clientWidth, document.body.clientHeight];
			}
			window.onresize = function() {
				document.getElementById("flashContainer").style.minWidth = flashWidth+"px";
				document.getElementById("flashContainer").style.minHeight = flashHeight+"px";
				document.getElementById("flashContainer").style.width = size[0] < flashWidth ? flashWidth+"px" : "100%";
				document.getElementById("flashContainer").style.height = size[1] < flashHeight ? flashHeight+"px" : "100%";
			};
			window.onload = function(){
				window.onresize();
			}
		}

This function accepts 2 parameters, the minimum width of your Flash movie and minimum height of your Flash movie. The script then calculates the viewable area of the browser window and sets an onResize listener to fire and set the minimum width and height of the flashContainer div.

Add this function above our SWFObject.embedSWF function call like so:

		<script type="text/javascript">
		//<![CDATA[

		function setFlashSize(flashWidth,flashHeight){
			var size = [0,0];
			if( typeof( window.innerWidth ) == 'number' ) {
				size = [window.innerWidth, window.innerHeight];
			} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
				size = [document.documentElement.clientWidth, document.documentElement.clientHeight];
			} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
				size = [document.body.clientWidth, document.body.clientHeight];
			}
			window.onresize = function() {
				document.getElementById("flashContainer").style.minWidth = flashWidth+"px";
				document.getElementById("flashContainer").style.minHeight = flashHeight+"px";
				document.getElementById("flashContainer").style.width = size[0] < flashWidth ? flashWidth+"px" : "100%";
				document.getElementById("flashContainer").style.height = size[1] < flashHeight ? flashHeight+"px" : "100%";
			};
			window.onload = function(){
				window.onresize();
			}
		}
		SWFObject.embedSWF("example.swf", "flashMovie", "100%", "100%", "6.0.0","expressInstall.swf");
		//]]>
		</script>

You’re SWF is now properly embedded with SWFObject, however there is a problem which often overlooked when creating full screen Flash web sites. If the browser window is smaller then the dimensions of the Flash SWF stage, no scrollbars will appear on the browser and the Flash stage will simply be cut off.

Resizing the browser window below the size of our Flash stage cuts off the edges of our Flash stage:

Step 5 Setting the minimum size of our Flash movie

Now we have our help functions we can call on them to set the minimum size of our Flash movie. This is as simple as adding the following line after our SWFObject.embedSWF call:

		setFlashSize(600,400);

Substitute the required width and height into the function parameters replacing the 600 and 400 respectively.

Congratulations!

Your HTML template should now look like this:

<!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>Flash SWF Embedding, the right way min-width, min-height and scroll bars</title>
		<meta http-equiv="flashMovie-Type" flashMovie="text/html; charset=iso-8859-1" />
		<style type="text/css" media="screen">
			html, body{
				margin:0;
				padding:0;
				height:100%;
			}
			#flashContainer{
				width:100%;
				height:100%;
			}
		</style>
		<script type="text/javascript" src="SWFObject.js"></script>
		<script type="text/javascript">
		//<![CDATA[

		function setFlashSize(flashWidth,flashHeight){
			var size = [0,0];
			if( typeof( window.innerWidth ) == 'number' ) {
				size = [window.innerWidth, window.innerHeight];
			} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
				size = [document.documentElement.clientWidth, document.documentElement.clientHeight];
			} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
				size = [document.body.clientWidth, document.body.clientHeight];
			}
			window.onresize = function() {
				document.getElementById("flashContainer").style.minWidth = flashWidth+"px";
				document.getElementById("flashContainer").style.minHeight = flashHeight+"px";
				document.getElementById("flashContainer").style.width = size[0] < flashWidth ? flashWidth+"px" : "100%";
				document.getElementById("flashContainer").style.height = size[1] < flashHeight ? flashHeight+"px" : "100%";
			};
			window.onload = function(){
				window.onresize();
			}
		}
		SWFObject.embedSWF("example.swf", "flashMovie", "100%", "100%", "6.0.0","expressInstall.swf");
		setFlashSize(600,400); //set the minimum width and height of your Flash movie
		//]]>
		</script>
	</head>
	<body>
		<div id="flashContainer">
			<div id="flashMovie">
				<h1>Alternative flashMovie</h1>
				<p><a href="http://www.adobe.com/go/getflashplayer"><img src="http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif" alt="Get Adobe Flash player" /></a></p>
			</div>
		</div>
	</body>
</html>

If you test this in your a browser and re size the browser window to something under 600px wide and 400px high you will see the scroll bars appear.

Extra Optimization: Setting the minimum size of our Flash movie

As a final step add alternate Flash content or SEOptimized content into the flashMovie div, for example:

		<div id="flashContainer">
			<div id="flashMovie">
				<h1>Alternate Content Goes Here!</h1>
				<p>Users without Flash or JavaScript, Search Engine Crawlers will see this text instead of my Flash movie</p>

				<p>Insert SEO Optimised keywords and information about your web site here for Search Engine indexing.</p>
			</div>
		</div>

Conclusion

You now have a Search Engine Optimized, flash embed page with provisions for users without Flash or JavaScript. And if visitor with a smaller resolution browser window then your Flash stage they will be able to scroll around the web site as if it were a standard HTML based site.

Further reading:

Embedding with SWFObject (official documentation)

Related posts:

Keep reading with more great tutorials and articles!

No related posts.

 

About the Author


Scott - who has written 1 posts on Flash Platform and ActionScript Tutorials.

My name is Scott King, I'm an Senior Interactive Developer with ten years of commercial development and design experience located in Sydney, Australia. I create Flash applications and web sites, Papervision 3D applications, rich media banners, strict W3C compliant CSS, xHTML and JavaScript/jQuery based web sites, PHP and MySQL back end applications and 32bit desktop applications and widgets. I also operate the official Papervision Showcase web site. http://www.scottking.com.au/blog http://www.papervisionshowcase.com

22 Comments For This Post

  1. flashcrobat Says:

    Hi, great article, nicely summarizing all essentials.
    Onlu thing I noticed, is that the Link you are providing to google.code is hardcoded with Uppercase letters, and therefore does not work. This would be the right one:
    http://code.google.com/p/swfobject/wiki/documentation

    Cheers
    Simon

  2. Patrick Welfringer Says:

    Nice write up :)

    I was about to code something similar myself for my current project, until I came across swffit: http://icanhaz.com/swffit

    swffit is a simple add-on to swfobject that seems to to the “minimum size” thingy quite nicely.
    It also lets you change your mind later on, if your size requirements vary during site navigation.

  3. arek Says:

    source link don’t work (404 error)

  4. Carlos Pinho Says:

    @arek,

    Pls link updated. Tnx 4 pointing it.

  5. Utopie - web design Liverpool Says:

    Wellll, I never knew embedding flash was so complex when needed to be done correctly. I have no interest in learning Flash becasue I think you need to specialise in it to be good, BUT, with all the free Flash templates etc floating around, I am tempted to have a little play in the future sometime…!

  6. yau Says:

    hi,
    i try your Flash_Embed_proper_usage.html,
    but it always showing the vertical Scrollbar even my window is as big as 1200x 1000,
    i using firfox 3.0.11

  7. Scott King Says:

    Hi Yau,

    Please amend your CSS like so:

    #flashContainer{
    width:100%;
    height:100%;
    overflow: hidden;
    }

    Add the overflow: hidden; property to the #flashContainer rule. This should fix your scrollbar problem.

    Let me know how you go.
    Scott

  8. emily Says:

    Really a great information on embedding of SWF movies. Most of my doubts have cleared.

  9. David Says:

    http://code.google.com/p/SWFObject/
    gives 404 error

  10. Carlos Pinho Says:

    @David

    Pls re-check!

  11. Simply Trojan Says:

    Hi Scott,

    Thanks for a great blog. We are a small flash website design & marketing company and are utilizing SWFOBJECT “alternate Content” for our many clients. Should we be concerned about being penalized by Google ?

    ~Trojan

  12. Edward Says:

    Great article. You surely make it look really simple. Easy to use and implement.

  13. evans Says:

    nice tut’.. but then i have one problem.. is there anywhere i can switch
    between the flash object and an image if the flash plugin is not found on the user’s machine.

  14. sako Says:

    Hi, in safari for windows it’ don’t work work. scrollbars not apper

  15. Tracy Says:

    Really great article… I am a novice, but wanted to learn about swfobject. I went through the tutorial, did as I was told, but when I open the html in a browser (firefox and safari, Mac), all I get is the alternate content. My swf, js, and other files are all in the same folder. I have flash installed… any idea what I am doing wrong? I can post my code if it would help.
    Thanks!!

  16. Kevin Calero Says:

    Hi All,

    Great post, but curious, when using the embedSWF method I have not found a way to set the wmode to transparent. Any ideas?

  17. LostMind Says:

    Hi!

    This is a great tutorial. But how can enable full screen? Like allowfullscreen: ‘true’ for swf. I can’t make it to work :-( . Please help me.

    Thanks!

  18. Ed Says:

    I tried setting the swfmode to transparent too. Would not work for me.

  19. padasalgi Says:

    Thanks for the most needed information.Despite following all your instructions I am unable to see ‘example movie’ in my html file.Please guide and advise me how to dispaly this swf movie in the html file.
    thanks

  20. jason Says:

    This is good although it does not work properly in Internet Explorer. I’m sure you’re alllll testing in IE? I’m using IE 8 to varying degrees of workingness i’ve tried ie 8 quirks mode ie 8 standards, ie 7 quirks and ie 7 standards and none really do what it does in Firefox / Chrome.
    Finding the height/width of a page in ie is a pain and i know i used to have a script somewhere that does it properly and if i find it i’ll try to pass along a fix to y’all.

  21. Ed Says:

    I have seen some drastic differences too while comparing IE7, IE8, Mozilla and Chrome. IE does things a lot differently at times which is very annoying.

  22. Stefano Says:

    Tks very much! After lose hours and hours finally someone that are able to write a simple y funcionally tutorial!
    Tks!!!

5 Trackbacks For This Post

  1. Twitted by unishwetabh Says:

    [...] This post was Twitted by unishwetabh [...]

  2. Down the Foxhole - News Flash Says:

    [...] For most Flash designers and developers, using SWFObject to embed Flash content on Web pages is a pretty straight forward task. However, understanding some finer details might help you to better understand the best practices of implementing SWFObject and to take advantage of some SEO techniques. Read TechLabs’ tips on implementing SWFObject. [...]

  3. Weekly Flash tutorial roundup #8 | Flash tutorials Says:

    [...] SWFObject and Best Practice Implementation [...]

  4. SWFObject 2.0 embedSWF width 100% not showing flash FIX Says:

    [...] [1] http://www.thetechlabs.com/tutorials/flash/swfobject-and-best-practice-implementation/ a great tutor… [2] http://code.google.com/p/swfobject/wiki/faq the official FAQ and this is #1 question [3] [...]

  5. Moje zajładki z pod “F” jak Flash | KonradKubiec.com Says:

    [...] http://www.thetechlabs.com/tutorials/flash/swfobject-and-best-practice-implementation/ – implementacja SWFObject dla opornych + zbiór najlepszych praktyk [...]

Leave a Reply