Tom Lauck’s Deseloper.org

Get Yourself a Flex Application Name

author:

While working with ExternalInterface on a recent Flex project it became a little cumbersome accessing the embedded SWF several times.  This is especially true if there is more than one SWF in a given page. Typing document.getElementById gets old fast as well. Therefore, this issue illustrates the need for core or global methods that can be reused, especially if you aren’t using a framework such as Prototype or jQuery.

When a SWF is embedded into a page, it naturally becomes a part of window.document/document/window (Internet Explorer).

The document is contained by the window object and may contain any number of elements.

Because of this, it is rather simple to access a SWF to pass a method into a Flex app.  The idea is simply to check whether or not the browser is IE (in this example we will use a UA check), then return the element with the appropriate SWF name:

function getSWFApp(appName) {
	if (navigator.appName.indexOf ("Microsoft") !=-1) return window[appName];
	else return document[appName];
}

Of course, there is more than one way to skin a cat. In this case, a more traditional approach:

function getSWFApp(appName) {
	return document.getElementById(appName);
}

Elsewhere in your code you can then pass a method to the SWF using dot syntax just as you would by using document.getElementById:

getSWFApp("ExampleApp").exampleEventName(exampleEventParams);

As an alternative to document.getElementById, returning the object from the document works like a charm. More importantly, a global method to retrieve a SWF saves many keystrokes. As an added bonus, this method can easily be added to the Flex HTML wrapper, thus allowing quick and easy deployment of applications using ExternalInterface.

7 Responses

date: November 28th, 2009

AH! This is the sort of thing I have been looking for. Doing some research for an article. You should add buttons to the bottom of your posts to digg, stumble, etc your content.- Pam

spoken by: Pam @ cyber monday deals

date: July 22nd, 2010

merhaba sitenize girdiğimde menü aşağıya kayıyor firefox kullanıyom. tesekkurler

spoken by: burun estetiği

date: October 17th, 2010

been reading your blog for 3 days. really love your posts. btw i am doing a research about this topic. do you know any other websites or maybe forums in which I might get more info? thanks in advance.

spoken by: Drew Diltz

date: November 28th, 2010

Wal-Mart Cyber Monday Kinect sales Microsoft Flip-Flops on Kinect Computer ‘Hack’: At this time leading to Cyber Monday, Amazon.com o…

spoken by: interior designers toronto

date: June 2nd, 2011

Fantastic weblog, lover! Get Yourself a Flex Application Name | Tom Lauck’s Deseloper.org is basically one thing. We are starting my own quickly and that i will certainly backup aspects of the one you have, lawfully naturally :)

spoken by: what is a hemroid

date: August 31st, 2011

I’ve read this post and if I could I want to suggest you few interesting things or tips. Maybe you can write next articles referring to this article. I want to read even more things about it!

spoken by: Glen Pefanis

date: January 14th, 2012

this was amazing. I dought if anybody else has the same information. thank you very much

spoken by: dorothycooper

Leave a Reply

Feb 27 2008