Getting the Flex BrowserManager working with SWFObject

Flex 3 introduces the BrowserManager object, which allows for deep linking URLs with SWF files. However, this functionality breaks when embedding flash files with the SWFobject library. This post presents a very simple solution to get it working again.

Background

You can take a look at the following other posts for some background and possible solutions for this problem:

http://www.themorphicgroup.com/blog/2008/05/28/using-flex-3-deep-linking-browsermanager-with-swfobject/

http://developer.iconara.net/objectlib/flex-browsermanager-swfaddress.html

The Problem

The history.js file, used by flex to access the web browser through an external interface, assumes that swf files are embedded like this:

html
embed
— object
— swf
/html

SWFObject embeds SWFs like this

html
— object
— swf
/html

Solution

Rather than adding code, this solution removes code. Since swfobject simplifies the code used to embed SWFs, it is only logical that the solution will simply do less.

Open your history.js file and comment out the lines in red like so

/* Get the Flash player object for performing ExternalInterface callbacks. */
function getPlayer(objectId) {
var objectId = objectId || null;
var player = null; /* AJH, needed?  = document.getElementById(getPlayerId()); */

if (browser.ie && objectId != null) {
player = document.getElementById(objectId);
}

if (player == null) {
player = document.getElementsByTagName(‘object’)[0];
}

/*
if (player == null || player.object == null) {
player = document.getElementsByTagName(’embed’)[0];
}
*/

return player;
}

function getPlayers() {
var players = [];
if (players.length == 0) {
var tmp = document.getElementsByTagName(‘object’);
players = tmp;
}
/*
if (players.length == 0 || players[0].object == null) {
var tmp = document.getElementsByTagName(’embed’);
players = tmp;
}
*/

return players;
}

After you save, you will see that SWFObject now works nicely with the Flex BrowserManager. Simple as pie…

One thought on “Getting the Flex BrowserManager working with SWFObject

  1. jason

    I tried your example and it does not work. I commented out the following and used this code and the example never finds the initial URL on loading the page. I am using IE7 and Firefox 3.0 not sure that matters? I do have some other methods and items being defined in initialize=”initializer(event)” not sure if that matters either.

    historyManagementEnabled=”false”
    creationComplete=”initApp()”

    [Bindable]
    private var fragment:String;

    public var browserManager:IBrowserManager;

    private function initApp():void {
    browserManager = BrowserManager.getInstance();
    browserManager.addEventListener(BrowserChangeEvent.URL_CHANGE, showURLDetails);
    browserManager.init(“”, “Welcome!”);
    }

    private function showURLDetails(e:BrowserChangeEvent):void {

    fragment = browserManager.fragment;
    }

    Reply

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s