Posted by Thurein on 22nd August 2009

SWFObject: Javascript Flash Player detection and embed script

Do you get the problem when you embedding flash (swf) file on your website with Adobe script ( automatically added from dreamwaver )?

<script type="text/javascript">
AC_FL_RunContent( 'codebase', 'http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,28,0', 'width', '940', 'height', '800', 'title', 'flashmo_template', 'src', 'flashmo_183_photo_rotator', 'quality', 'high', 'pluginspage', 'http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash', 'bgcolor', '#FFFFFF', 'movie', 'flashmo_183_photo_rotator' ); //end AC code
</script>

You can solve easily with swfobject javascript embed script.
SWFObject is a small Javascript file used for embedding Adobe Flash content. The script can detect the Flash plug-in in all major web browsers (on Mac and PC) and is designed to make embedding Flash movies as easy as possible.

How it works?

Using SWFObject is easy. Simply include the swfobject.js Javascript file, then use a small amount of Javascript on your page to embed your Flash movie. Here is an example showing the minimum amount of code needed to embed a Flash movie:

<script type="text/javascript" src="swfobject.js"></script>

<div id="flashcontent">
This text is replaced by the Flash movie.
</div>

<script type="text/javascript">
var so = new SWFObject("movie.swf", "mymovie", "400", "200", "8", "#336699");
so.write("flashcontent");
</script>

You can pass arguments what you want.

var so = new SWFObject(swf, id, width, height, version, background-color [, quality, xiRedirectUrl, redirectUrl, detectKey]);

Create a new SWFObject and pass in the required arguments:

  • swf – The file path and name to your swf file.
  • id – The ID of your object or embed tag. The embed tag will also have this value set as it’s name attribute for files that take advantage of swliveconnect.
  • width – The width of your Flash movie.
  • height – The height of your Flash movie.
  • version – The required player version for your Flash content. This can be a string in the format of ‘majorVersion.minorVersion.revision’. An example would be: “6.0.65″. Or you can just require the major version, such as “6″.
  • background-color – This is the hex value of the background color of your Flash movie.

You can also pass optional arguments like :

<script type="text/javascript">
var so = new SWFObject("movie.swf", "mymovie", "400", "100%", "8", "#336699");
so.addParam("quality", "low");
so.addParam("wmode", "transparent");
so.addParam("salign", "t");
so.write("flashcontent");
</script>

Optional arguments are:

  • quality – The quality you wish your Flash movie to play at. If no quality is specified, the default is "high".
  • xiRedirectUrl – If you would like to redirect users who complete the ExpressInstall upgrade, you can specify an alternate URL here
  • redirectUrl – If you wish to redirect users who don’t have the correct plug-in version, use this parameter and they will be redirected.
  • detectKey – This is the url variable name the SWFObject script will look for when bypassing the detection. Default is ‘detectflash’. Example: To bypass the Flash detection and simply write the Flash movie to the page, you could add ?detectflash=false to the url of the document containing the Flash movie.

If you don’t have any knowledge about javascript you may download generator or you may get online generator to generate html, javascript codes .

  • About Thurein Soe
    Visit Thurein's website.

    Freelance Web Developer. Founder of The Tech Space. Follow me on Twitter.

    1. 5 Responses

    2. sreekanth says:

      Nice example.

      Please can you let me know that you are adding so.write().what the need of this.In swfobject.js there no function like write().Does we need to explicity add this function in swfobject.js

      Please clarify my doubt.

       var so = new SWFObject("movie.swf", "mymovie", "400", "100%", "8", "#336699");
            so.addParam("quality", "low");
            so.addParam("wmode", "transparent");
            so.addParam("salign", "t");
             so.write("flashcontent");
      
    3. Thurein says:

      Yes, You have to add so.write(). It’s mean tell SWFObject to replace html tag “div id=’flashcontent’” to replace with Flash Content

    4. sreekanth says:

      yes,but in SWFObject2.2 not supporting so.write()

      how can we support this in new version.

    5. Thurein says:

      I hope this link will be helpful for you .

      http://www.gotoandlearn.com/play?id=77

    6. sreekanth says:

      Thanks for your quick attention.

      When go through the swfobject2.2 api.There are two was to add alternate content.
      one is static publishing.

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

      The another way is dynamic publishing,syntax as fallows.

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

    Post your comments