All Your Base Are Belong To Us

Posted in "Flash, Flex, General" at 5:00 am on August 11, 2008 by Alaric Cole |

 You’be probably heard of the many available SWF embed parameters, such as allowNetworking, wmode, allowScriptAccess, and of course height and width. But there a few less well-known ones that may be incredibly useful.

One of these optional attributes you can specify when embedding a SWF is named base. Adobe’s LiveDocs give the following description of the attribute:

base - Specifies the base directory or URL used to resolve relative path statements in ActionScript.

This is important when dealing with relative paths (URLs) in Flash. By default, the base URL of a SWF is its parent HTML page. This means if your SWF is loaded from another domain and has assets such as images or XML files, you’re in trouble.

Say, for instance, that yahoo.com loads a SWF ad from ads.com/car.swf. That SWF, in turn, tries to load “images/convertible.jpg”, using a relative path to an images directory that resides on ads.com. Given the default settings, this will not work, unless “yahoo.com/images/convertible.jpg” exists. To get around this, developers have often used absolute paths, such as “http://ads.com/images/convertible.jpg”. The base parameter solves this problem, allowing you to continue to use relative paths.

To allow relative paths, all you need to do is “reset” the base directory. You can even use base="." to mean “make all paths relative to the swf itself.”

Here’s an example of a full embed statement:

<object classid=”clsid:d27cdb6e-ae6d-11cf-96b8-444553540000″
width=”500″
height=”300″
codebase=”http://active.macromedia.com/flash7/cabs/swflash.cab#version=9,0,2,8″>
<param name=”base” value=”.”></param>
<param name=”movie” value=”someSWF.swf”></param>
<param name=”play” value=”true”></param>
<embed base=”.” src=”someSWF.swf”
width=”500″ height=”300″ play=”true”
pluginspage=”http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash”>
</embed></object>

If you are fond of the popular SWFObject JavaScript library for embedding Flash content, here’s how to use the base parameter with it:

var swfobj = new SWFObject(value, “someSWF”, 500, 300, “9.0.28″, “#FFFFFF”);
swfobj.addParam(”base“, “.“);
swfobj.write(”swfDiv”);

Note that if you have a SWF that loads another SWF within itself, the base path of the loaded SWF will always be the loading SWF.

Share: on Yahoo! My Web | on del.icio.us | digg it! | reddit!

7 Comments »

RSS feed for comments on this post. TrackBack URI

  1. I love reading quick tips like this. Thanks for sharing!

    Comment by Nate Koechley — August 11, 2008 #

  2. Excellent! Thanks!

    Comment by Ethan.Du — August 11, 2008 #

  3. Shouldn’t base be in a PARAM element rather than an attribute of OBJECT?

    <param name="base" value="." />

    Comment by Steve Clay — August 12, 2008 #

  4. You’re correct. That’s now updated. I got way too used to using SWFObject.

    Comment by Alaric Cole — August 12, 2008 #

  5. is there way to get the base param setted inside the movie using AS?

    Comment by Mauricio úzüga — September 22, 2008 #

  6. While you can access parameters passed in through the query string using loaderInfo.parameters, there’s not a standard way of accessing embed parameters through ActionScript. Though if you really needed to you could parse the html page that embeds the swf, as long as it’s valid xhtml.

    Comment by Alaric Cole — September 24, 2008 #

  7. Great! Learned something new again! Thanks for the example!

    Comment by Billigflüge — October 14, 2008 #

Leave a comment

Note: Comments are moderated for first-timers. Spam deleted.

XHTML: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>

Hosted by Yahoo!

Copyright © 2007 Yahoo! Inc. All rights reserved. Privacy Policy - Terms of Service

Powered by WordPress on Yahoo! Web Hosting.