Flex Parametrized Data: Browser Compatibility
How to pass request from server-side script like ASP, PHP, or JSP to Flex?
In Flex, MXML applications are rendered as SWF that is embedded normally within HTML pages but can also be placed within any server-side pages. Since Flex mainly deals with the presentation part, usage of FlashVars within embed and object tag (HTML-based) provide an excellent mechanism for establishing the communication between Flex and server-side scripts.
FlashVars Properties:
The FlashVars properties do not need to be static. We can even embed dynamic data within the Embed or Object placed within the script tags.
The values of the FlashVars properties must be URL encoded. The format of the string is a set of name-value pairs separated by an ampersand (&). We can escape special and non-printable characters with a percentage symbol (%) followed by a two-digit hexadecimal value. We can represent a single blank space using the plus sign (+).
Browser-Compatibility Issue with FlashVars:
The main problem that is faced realtime is that the parameter passed in FlashVars when used in Embed tag alone remains inaccessible from IE and Mozilla higher-end versions, say, Mozilla 3.5.
Solution for this Browser-Compatibility Problem:
This problem can be overcome by providing the FlashVars inside both Embed tag and Object tag as follows:
//home.jsp
<%
String mydata=request.getParameter(“uname”);
%>
<object classid=”clsid:D27CDB6E-AE6D-11cf-96B8-444553540000″
id=”home” width=”100%” height=”100%”
codebase=”http://fpdownload.macromedia.com/get/flashplayer/current/swflash.cab”>
<param name=”movie” value=”home.swf” />
<param name=”quality” value=”high” />
<param name=FlashVars value=”mydata=<%=mydata%>”/>
<param name=”bgcolor” value=”#869ca7″ />
<param name=”allowScriptAccess” value=”sameDomain” />
<embed src=”home.swf” quality=”high” bgcolor=”#869ca7″
width=”100%” height=”100%” align=”middle”
play=”true”
loop=”false”
quality=”high”
allowScriptAccess=”sameDomain”
type=”application/x-shockwave-flash”
pluginspage=”http://www.adobe.com/go/getflashplayer”
FlashVars=”mydata=<%=mydata%>”>
</embed>
</object>
Note: In the above code, we give the parameter name as “mydata” and its value is set dynamically by the server-side code. The value can be either passed via request from some other server pages or retrieved from DB or set by some other means.
Accessing FlashVars properties from Flex:
Flex has a top-level class Application which has property parameters that return an Object containing name-value pairs representing the parameters provided to this Application. The sources of parameters could either come in the form of query string of the Application’s URL or the value of FlashVars parameters.
In the above scenario, our MXML script for accessing FlashVars would go like this:
<mx:Script>
<![CDATA[
private var dataFromServer:String;
private function init():void
{
dataFromServer=Application.application.parameters.mydata;
}
]]>
</mx:Script>
Note: The above Flex method init() can be invoked from any event-based component to access the value of the variable.
Know more about our Flex Application services here. To get a free quote for Flex Application development, contact us.