`
qsbj84qsbj
  • 浏览: 13395 次
社区版块
存档分类
最新评论

Flex与Javascript之间的交互

阅读更多

  最近做的一个项目要用到Flex与Javascrip之间的交互,所以花时间学习了一下。在
  网上找了好多关于这方面的文章,但是介绍的都不是很全面,主要是例子程序写的不是很
  好,很多都还存在一些问题,download下来后,发现还要做些修改才能运行。
  先分别简要介绍一下Flex调用Javascript函数和Javascript调用Flex函数。
  1. Flex调用Javascript函数
  Flex通过使用ExternalInterface.call()函数来调用Javascript中的方法,此函数的原型为
  ExternalInterface.call(function_name:String, parameter:String),参数function_name
  是调用的javascript的函数名,parameter是Javacript函数需要的参数。此函数还可以有返回值
  ,也就是说Javascript函数可以返回一个结果给ExternalInterface.call()这个函数调用.ExternalInterface
  封装了对浏览器支持的检查,可以用available属性来查看。
  先举个简单的例子:
  Javascript函数:
  function sayHelloFromJs(message)
  {
  alert(message);  //message是由flex端传过来的
  return "echo from javascript:" + message; //返回给Flex端的消息
  }
  Flex调用:
  var str:String = ExternalInterface.call("sayHelloFromJs","Hello,Jav ascript.");
  Alert.show(str); //显示javascript端返回的消息
  2. Javascript调用Flex函数:
  Flex端需要注册和页面交互的Javascript方法。通过ExternalInterface的addCallback()函数实现,
  addCallback()函数的原型为addCallback(js_function:String, flex_function:Function),第一个参数
  js_function是Javascript可以调用的方法名称,第二个参数flex_function是Javascript回调的Flex方法.
  举个简单的例子:
  Flex端:
  public function sayHelloFromFlex(message:String):String
  {
  Alert.show(message); //Javascript端传过来的消息
  var str:String = "echo from flex:" + message;
  return str; //返回给Flex端的消息
  }
  public function initApp()
  {
  ExternalInterface.addCallback("sayHelloFromFlex",s ayHelloFromFlex);//注册与页面交互的方法
  }
  Javascript端:
  在javascript端首先要获取swf对象的引用,我们假设我们已经拿到了该引用,为MyFlexApp.
  swf的获取将在下面专门介绍。
  
  function testFlexFunc()
  {
  var strMessage = MyFlexApp.sayHelloFromFlex("Hello, Flex");
  alert(strMessage); 
  }
  
   TestFlexFunc
  3.  Javascript端获取swf对象的引用:
  在Javascript代码中,我们首先要定义一个object标签对象,object标签对象的大概格式
  如下:
  height='480'
  name='test' id='MyFlexApps'>
  
  
  
  
  
  height="378"
  name ="FlexObject"
  play="true"
  loop="false"
  allowScriptAccess="sameDomain" 
  type="application/x-shockwave-flash"
  pluginspage="http://www.adobe.com/go/getflashplayer">
  
  
  定义好这个节点标签后,我们还需要在写一个JS函数来获取该swf对象的引用。
  function getSWFObject(movieName)
  {
  if(document[movieName])
  {
  return document[movieName];
  }else if(window[movieName]){
  return window[movieName]; 
  }else if(document.embeds && document.embeds[movieName]){
  return document.embeds[movieName];
  }else{
  return document.getElementById(movieName);
  }
  }
  然后上面2中的testFlexFunc()函数的那条调用Flex函数的语句可以改写为: var strMessage = getSWFObject["MyFlexApps"].sayHelloFromFlex("Hello , Flex"); 最后给出一个完整的例子供大家参考,包括Flex端和JS端的完整代码:
  Flex代码:
  
  height="160">
  
  Flex: "+ message; 
  return str;
  }
  public function initApp():void { 
  Security.allowDomain("*");
  Security.allowInsecureDomain("*");
  ExternalInterface.addCallback("sayHelloFromFlex",s ayHelloFromFlex);
  }
  public function invokeJsFunc():void {
  var str:String = ExternalInterface.call("sayHelloFromJs","Hello,Jav ascript.");
  Alert.show(str);
  }     
  ]]>
  
  
  
  Html代码:
  
   
  
  
  function sayHelloFromJs(value){
  alert(value);
  return "Echo from Js: " + value;
  }
  function invokeFlexFunc(){
  var message = "Hello,Flex.";
  var str = getSWFObject("MyFlexApps").sayHelloFromFlex(messag e);
  alert(str);
  }
  function getSWFObject(movieName)
  {
  if(document[movieName])
  {
  return document[movieName];
  }else if(window[movieName]){
  return window[movieName]; 
  }else if(document.embeds && document.embeds[movieName]){
  return document.embeds[movieName];
  }else{
  return document.getElementById(movieName);
  }
  }
  
  height='160'
  name='test' id='MyFlexApps'>
  
  
  
  
  
  
  
  
  
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics