Archive for August, 2006

Flex 2: How to get URL query-string variables within Flex application

This has been a reoccurring problem on flexcoders and in the small circle of Flex developers I know. It is a problem that I had encountered and thought that I had found an answer; Abdul Qabiz's Blog posting (duplicated) had described a (in his own words) "quick-and-dirty" solution. That method works well for pulling the String value of the parameters passed in the query-string, but what do you do when those values are URL Encoded? Below is my (and I use my very loosely as it isn't much to claim) solution that provides similar behavior as described in Abdul's posting, but requires significantly less code and is much more readable:

Source and Usage

Given the URL:
http://deepthoughts.orsomethinglikethat.com?myParam=My%20Value

the return value is "My Value".

This may not be the ideal solution if the URL is pointed directly to the SWF or if you choose to make your parameters available via FlashVars.

Token Phrases

There is a new page containing a compilation of my favorite "Token Phrases." The phrases themselves may not be of any special meaning to anyone but myself and a handful of others, but the definitions themselves may touch your heart.

It's a work in progress so feel free to comment to this post with your favorite phrases and their universally understood definitions.

Flex 2: addExternalEventListener( … )

I've recently been looking for a way to invoke an ActionScript/Flex function based on a browser event… say window.unload (orsomethinglikethat). The ability to hook into Application creationComplete is present, however I am unaware of any built-in Flex/FlashPlayer unload/shutdown hook points.

I’ll now turn my attention to letting the browser notify the Flex Application of events. There are a couple of ways to accomplish this, both of which involve using the flash.external.ExternalInterface.addCallBack() to expose a Flex function to the containing browser:

    • Embed or import JavaScript in the Flex application’s HTML wrapper to hard code the browser event handlers to invoke the exposed Flex callback functions.
    • Assign the browser event handlers in the Flex code.

Both options have a time and place, but for my purposes, I’ll choose the latter option. I prefer not to maintain additional JavaScript and/or custom HTML wrapper code, and ideally I’d like to be able to directly map a browser event to a Flex function.

Source and Usage

Steps #1 & 3 are uninteresting and strictly interact with ExternalInterface.
Step #2 is a bit more interesting as it involves constructing the JavaScript statement that binds the browser event to the callBackAlias.
Since ExternalInterface.call() uses the functionName parameter as a pointer to a function that has been defined in the tag, it is not possible to pass in a String of JavaScript as we would with the JavaScript eval() function. Instead we “wrap” our JavaScript in an anonymous function to be executed upon ExternalInterface.call() invocation. The script we have just “wrapped” assigns the browser event handler to another anonymous function that contains the script to invoke the provided Flex callBack function by the provided alias.

Categories

  • Blogroll

  • Monthly Archives