Tomás Lin’s Programming Brain Dump

Flex, Grails, Facebook, iPhone and all that Jazz

Getting the Flex BrowserManager working with SWFObject

with one comment

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…

Written by Tomas Lin

July 5, 2008 at 4:04 am

One Response

Subscribe to comments with RSS.

  1. 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;
    }

    jason

    August 12, 2008 at 3:43 pm


Leave a Reply