How to pass data to a Flex application using SWFObject 2.0

 

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 ?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值