02.18.08

SWFObject.addVariable() in AS3

Posted in Embedding, Flash, Flex at 7:00 am by Curtis J. Morley

SWFObject.addVariable() in AS3

SWFObject is the standard for embedding Flash files into HTML. If you haven't used it run on over to Geoff Stearns site and download yourself a heapin' helpin' of some SWFObject. If you are feeling especially adventuresome and want to test out the latest features in the new SwfObject 2.0 then head over to the SwfFix development blog. SWFObject eliminates the need to click on the Flash piece on a website before being able to interact with it, it has plug-in detection, it can be used with popular blogging software on sites such as this, and there are plugins for Dreamweaver and as a publish setting right in Flash. You should be using this for every website you build.  As of the 15th of February (I wonder what they were doing on Valentines Day) you can even download an SWFObject Adobe Air app for generating the proper code to embed your SwfObject into your website.  It employs the new SWFObject 2.0.  Or for quick and easy access just use the web version to generate your swfObject code

Now that you are totally sold and will never build anything Flash without it, you may want to inject variables (FlashVars) into your swf.  The methods are different for AS2 and AS3.  It is quite easy, yet different in each of these versions of ActionScript.  You simply throw the following code into the HTML and then ...

<div id="flashcontent">
Swf Audio is not loaded Please check your path. this should be an audio control built in flash.</div>

<script type="text/javascript">

var so = new SWFObject("SoundControlTest2.swf", "audioControl", "240", "24", "8", "#EFECDD");
so.addVariable("audioURL", "AllThatIWant.mp3");
so.addVariable("audioURL2", "Comfort.mp3");
so.addVariable("audioURL3", "Jolene.mp3");
so.addVariable("audioURL4", "Amazing.mp3");
so.write("flashcontent");
</script>

...then in your Document class (or on the MainTimeline if you are not using a Document Class) you will need to reference the parameters property of the loaderInfo property like this:

myTextBox.text = this.loaderInfo.parameters.audioURL;

In AS2 you only can access the variable directly on the mainTimeline using something like this:

myTextBox.text = audioURL;

Hope this helps you (and your users) have a better Flash experience on the web.

As always Happy Flashing.

Curtis J. Morley

13 Comments »

  1. Oliver said,

    December 14, 2008 at 6:50 pm

    this is a pretty crappy explaination. what am I supposed to do with “this.loaderInfo.parameters.audioURL;”?
    can I use it as:
    myTextBox.text = this.loaderInfo.parameters.audioURL;
    A better working example would have been more useful.

  2. Aubby said,

    January 7, 2009 at 5:34 pm

    I wouldn’t call this a “crappy” explanation.

    This gets you 90% of the way there, a simple google on the line “this.loaderInfo.parameters” would have gotten you the rest of the way. Stop whining and do your research.

    Thank you very much Curtis.

    I do have one question: Are SWFObject.addVariable and SWFObject.addParam synonymous?

  3. Aubby said,

    January 7, 2009 at 5:35 pm

    SWFObject is sweet, btw. No need to sell me. ^_^

  4. Curtis J. Morley said,

    January 12, 2009 at 11:05 am

    Aubby,

    Great question. The difference between addParam and addVariable is that addParam allows you to change the swf within the HTML and addVariables allows you to pass in variables through FlashVars.

    Examples of changing the parameters are:
    so.addParam(“quality”, “low”); – Low Quality rendering int he swf
    so.addParam(“wmode”, “transparent”); Allows the background to be transparent.
    etc…

    I hope this helps.

    Thanks,
    Curtis J. Morley

  5. Curtis J. Morley said,

    January 13, 2009 at 8:24 am

    Oliver,

    Thanks for the comment. It seems that you have a valid comment. You could have said it a little nicer, but I added what was missing.
    Thanks for visiting the site.

    Curtis J. Morley

  6. nick said,

    June 23, 2009 at 7:34 pm

    Thanks Curtis J. Morley !

  7. Joe said,

    August 3, 2009 at 12:41 pm

    Thank you for posting this info. I scoured the internet for 6 hours looking for this one little piece of information. Every other site was like. It’s so easy you just add this to your HTML and that’s it.

    There seemed to be a mass amount of forgetting to add the key to how it actually works.

    Now my bosses temperature will drop and I can feel like I’m doing a good job again.

    THANKS SO MUCH FOR BEING THOROUGH.

  8. liuhuan said,

    August 18, 2009 at 4:38 am

    Thanks a lot:)

  9. Bruce E said,

    August 25, 2009 at 2:49 pm

    Thank you for this (it’s not crappy at all). It helped me finish my project. Thanks again.

  10. xml per swj.Obj.addVariable in Flash auslesen! - Flashforum said,

    December 23, 2009 at 5:00 am

    […] curtismorley.com SWFObject.addVariable() in AS3 […]

  11. PixelMaker said,

    January 13, 2010 at 10:13 am

    Hi Curtis,

    Thank you very much for sharing it.

    I am PHP programmer and now I am learning flash actionscript. This helped me a lot.

    Thanks again,
    -PM.

  12. ashi said,

    March 6, 2010 at 3:27 am

    Hi

    I have got the working source code from here :

    http://www.flexflashforum.com/viewtopic.php?f=5&t=987&p=1097#p1097

    Hope it will help you all.

  13. Ashish said,

    March 6, 2010 at 4:11 am

    Hi

    Download a working sample application from here :

    http://www.flexflashforum.com/download/file.php?id=3

Leave a Comment