Problem
I want to display a Flex application in my webpage and pass it some parameters?
Solution
I'll use the swfobject project which will allow me to display my application and pass it some parameters really properly
Detailed explanation<解决方法:>
Here is my Flex application, have you can see the "parseParameters" method is important. It will display the name and the value of every parameters I passed to my application :
package com.palleas
{
import mx.controls.TextArea;
import mx.events.FlexEvent;
import spark.components.Application;
public class Facade extends Application
{
protected var logBox:TextArea;
public function Facade()
{
super();
logBox = new TextArea();
logBox.width = 500;
logBox.height = 300;
addEventListener(FlexEvent.CREATION_COMPLETE, creationCompleteHandler);
}
protected function creationCompleteHandler(e:FlexEvent):void
{
removeEventListener(FlexEvent.CREATION_COMPLETE, creationCompleteHandler);
addElement(logBox);
parseParameters();
}
/**
* This method display the name and the value
* of every parameters passed to the Flex Application
*/
protected function parseParameters():void
{
logBox.text = "";
var currentParamIndex:uint = 1;
for(var parameterName:String in parameters)
{
logBox.text += "Parameter #"+currentParamIndex + ": ";
logBox.text += parameterName + " " + parameters[parameterName] + "/n";
currentParamIndex++;
}
}
}
}
My webpage is really cheap, all I want is my flex Application so :
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="fr" lang="fr">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Load some parameters</title>
</head>
<body>
<div id="componentBox">
<!-- alternate content -->
<p>Woops, it seems you don't have Flash player installed, shame on you! ;-)
</div>
<script type="text/javascript" src="js/swfobject/swfobject.js"></script>
<script type="text/javascript">
window.onload = function()
{
var params = {
cookbook : "Flex",
author : "Romain 'Palleas' Pouclet",
version : "1.0",
website : "http://www.adobe.com",
air : "best technology ever!"
}
/*
* Loading a SWF to my webpage
* parameter #1 is the path to the SWF I want to load
* parameter #2 is the id of the HTML container (here it's a div containing alternative content (in case Flash in not installed
* or javascript is not activated
* parameters #3 and #4 are the dimension of the application (here 500px x 500px)
* parameter #4 is the required version to make the application work
* parameter #5 is path to the express installer (it will install flash)
* parameter #6 is an anonymous object containing my parameters
*/
swfobject.embedSWF("Parameters.swf","componentBox","500","500","9.0.0","js/swfobject/expressInstall.swf", params);
}
</script>
</body>
</html>
Conclusion
As you can see, swfobject makes really easy to add a Flex application to a webpage and pass it some parameters properly. You don't have to write the object, embed, params HTML tags by yourself anymore, how cool is that ?