Set Application State From Flex to URL

Although this didn't turn out to be difficult by any means, I thought I'd throw it up anyways...

I came across the requirement to save the current "view" or "state" into the URL (much like Google Maps does with their "Link to this page" feature) for my Flex 2.0 application. I ended up throwing together a solution that works the same way as Yahoo Maps does, using the a hash in the URL string.

From my Flex app, I added a function which called an external JavaScript function to reset the URL with the values I wanted added...

App.mxml (excerpt)
----------------------------------------------
private function changeDocumentURL():void
{
var urlString:String = "var1=test1&var2=test2";
ExternalInterface.call("myJSFunction", urlString);
}


Inside my HTML wrapper for my swf, I add my JS function that's getting called.

App.html (excerpt)
----------------------------------------------
function myJSFunction(newString)
{
location.replace('#' + newString);
}


When I make the call for changeDocumentURL() it sets the string of variables in urlString to the browsers url (but DOESN'T reload the page)

Browser URL now looks like: http://www.visualconcepts.ca/App.html#var1=test1&var2=test2

Now for the flip side... looking into the script used to initiate the swf object in our HTML wrapper page App.html there is already an entry for flashvars which we can simply append to:

App.html (inside the AC_FL_RunContent() JS function)
----------------------------------------------
var preLoadFlashVars = location.hash ? location.hash.substr(1) : '';
"flashvars",'historyUrl=history.htm%3F&lconid=' + lc_id + '&' + preLoadFlashVars


Back inside App.mxml file, accessing the input vars is simply done using:

App.mxml (excerpt)
----------------------------------------------
Application.application.parameters.var1
Application.application.parameters.var2


So simple, but so cool!

Comments
Hi Brendan,

Out of curiosity... What does your Javascript function look like? In other words, what javascript code do you use to write to the hash?
# Posted By Aaron King | 3/26/07 2:19 PM
Hey Aaron,

I copied my old posts over from my previous site, and looks like this one didn't copy properly. I've fixed now, and you should see the rest of the post :-) sorry for the issue...

Brendan
# Posted By Brendan | 3/26/07 2:58 PM
Out of curiosity... What does your Javascript function look like? In other words, what javascript code do you use to write to the hash?
# Posted By airline tickets | 6/7/07 12:32 PM
Hey Aaron,

I copied my old posts over from my previous site, and looks like this one didn't copy properly. I've fixed now, and you should see the rest of the post :-) sorry for the issue...
# Posted By lexus | 6/13/07 2:18 PM
Hi Brendan,

Out of curiosity... What does your Javascript function look like? In other words, what javascript code do you use to write to the hash?
# Posted By al | 7/8/07 9:55 AM
We specialize in http://www.batterylaptoppower.com laptop batteries,laptop AC adapters. All our products are brand new, with the excellent service from our laptop battery of customer service team.
the most convenient and cheap replacement battery online shop in uk. We specialize in http://www.batteryfast.com laptop batteries,laptop AC adapters. All our products are brand new, with the excellent service from our customer service team.
the most convenient and http://www.batteryfast.co.uk cheap replacement battery online shop in uk. We specialize in laptop batteries,laptop AC adapters. All of batteries are brand new, with the excellent service from our customer service team, you can feel free to purchase on laptop battery!
Here is cheap laptop ac adapter online shop in uk. We specialize in http://www.adaperlist.com laptop AC adapters. All our products are brand new, with the excellent service from our customer service team.
# Posted By power laptop battery | 9/23/08 5:57 PM
Contact information, staff directory, registry database, calendar, disciplinary actions, fees, forms, Nurse Practice Act and newsletters available.
School of Nursing. Offers undergraduate and graduate degrees. Includes news, description of programs, and requirements.

http://dawnmor.101freehost.com/nursing6272.html
http://dawnmor.101freehost.com/nursing1711.html
http://dawnmor.101freehost.com/nursing7716.html
http://dawnmor.101freehost.com/debt3558.html
http://dawnmor.101freehost.com/nursing2542.html
http://dawnmor.101freehost.com/nursing8660.html
http://dawnmor.101freehost.com/of5251.html

Welcome to the School of Nursing. Welcome to the School of Nursing web-site. We invite you to become familiar with our various programs, initiatives and the work of our faculty
Dedicated to promoting nursing opportunities across a wide range of fields, and development and coordination of nursing education, research and professional issues.

http://dawnmor.gigazu.com/nursing5312.html
http://dawnmor.gigazu.com/nursing498.html
http://dawnmor.gigazu.com/nursing2776.html
http://dawnmor.gigazu.com/debt6283.html
http://dawnmor.gigazu.com/nursing9680.html
http://dawnmor.gigazu.com/nursing7094.html
# Posted By Kartograf | 9/25/08 2:30 AM
BlogCFC was created by Raymond Camden. This blog is running version 5.005.