AS與JS相互連信(Flex中調用js函數)

來源:互聯網
上載者:User

標籤:style   blog   http   io   ar   color   os   使用   sp   

Flex中As調用Js的方法是: 
     1、匯入包 (import flash.external.ExternalInterface;) 
     2、使用ExternalInterface.call("Js函數名稱",參數)進行調用,其返回的值就是Js函數所返回的值 

Js調用As的方法是: 
     1、匯入包 (import flash.external.ExternalInterface;) 
     2、在initApp中使用ExternalInterface.addCallback("用於Js調用的函數名",As中的函數名)進行註冊下 
     3、js中 就可以用document.getElementById("Flas在Html中的ID").註冊時設定的函數名(參數)進行調用 


as和js通訊addcallback失效 
參考原文:http://www.zhaohongri.cn/?p=14 

情況一:flash一旦在瀏覽器裡cache住,如果在as裡一開始就addcallback就會失效 
情況二:一個js函數上來就調用as的一個函數的時候,頁面會報錯,提示找不到這個flash對象,或者函數沒有定義。Flash8的時代,針對 ExternalInterface這個類,文檔裡只說明了怎麼用,而沒有具體說怎麼合理的組織和頁面的結構,一直到了cs3的時代,協助裡才說明了正確的函數註冊和js調用的過程,具體的見Flash cs3協助。大概的代碼如下: 

js部分: 

           <script> 
var jsReady=false; 
var swfReady=false; 

function isReady(){ 
return jsReady; 
} 

function setSwfIsReady(){ 
      swfReady=true; 
   getSWF(”flashobj”).fun() 

} 

function pageInit(){ 
     jsReady=true; 
} 

function getSWF(movieName) { 
   if (navigator.appName.indexOf(”Microsoft”) != -1) { 
    return window[movieName+”_ob”]; 
   } else { 
    return document[movieName+”_em”]; 
   } 
} 

onload=function(){ 
   pageInit(); 
} 

</script> 

注意,在getSWF函數裡用了 return window[movieName+”_ob”]和return document[movieName+”_em”],在IE下,如果object標籤和embed表現用同樣的id,通過js去訪問flash對象的時候,IE會認不出,FF是沒有問題的 

as部分 

private function registerJsFun():void{ 
       if(ExternalInterface.available){ 
     try{ 
      var containerReady:Boolean=isContainerReady(); 
      //ExternalInterface.call(”ceshi”,”registerJsFun:”+containerReady); 
      if(containerReady){ 
       //註冊函數 
       setupCallBacks(); 
      }else{ 
       //檢測是否準備好 
       var readyTimer:Timer=new Timer(100); 
       readyTimer.addEventListener(TimerEvent.TIMER,timeHandler); 
       readyTimer.start(); 
      } 
     }catch(error:Error){ 
      trace(error) 
     } 
    }else{ 
     trace(”External interface is not available for this container.”); 
    } 
   } 
  
private function timeHandler(event:TimerEvent):void{ 
var isReady:Boolean=isContainerReady(); 
      if(isReady){ 
   Timer(event.target).stop(); 
   setupCallBacks(); 
} 
} 

private function isContainerReady():Boolean{ 
var result:Boolean=Boolean(ExternalInterface.call(”isReady”)); 
return result; 
} 

private function setupCallBacks():void{ 
       ExternalInterface.addCallback(”fun”,fun); 

        ExternalInterface.call(”setSwfIsReady”); 
     } 

具體我就不解釋了,不明白的可以仔細去看下cs3協助,大概的意思就是頁面開始渲染的時候js去調用swf對象,有可能swf對象沒有完全 load完,所以這個觸發器要從flash開始,當flash載入的時候就開始不停的調用頁面的一個函數,取一個頁面是否載入完畢的標識,當 pageonLoad後,這個標識為true了,說明flash也載入完畢了,這個時候flash再開始註冊函數,同時調用頁面的js,讓js調用 Flash對象 




執行個體:a.mxml 


<?xml version="1.0" encoding="utf-8"?> 
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"> 
    <mx:Script> 
        <![CDATA[  
      import flash.external.*; 
      public function asFunc():String { 
          return sending_ti.text; 
      } 
      
      public function initApp():void { 
        //AddCallback方法允許javascript調用flash時間上函數 
          ExternalInterface.addCallback("flexFunctionAlias", asFunc); 
      } 
      
         public function callWrapper():void {      
             var f:String = "changeDocumentTitle"; 
       //ExternalInterface.call(functionName:String,Parameters)可以調用javascript函數 
       //參數1: functionName – 你想要調用的javascript函數名要以字串的形式 
       //參數2: Parameters – 需要傳遞給javascript函數的參數,用逗號分開,是可選的。              
             var getJSValue:String = ExternalInterface.call(f,"New Title"); 
             received_ti.text = getJSValue; 
        } 

        ]]> 
    </mx:Script> 
    <mx:Button id="send_button" x="368" y="100" click="initApp();" label="發送" fontSize="12" width="62"/> 
    <mx:TextInput id="received_ti" x="148" y="62" width="203" fontSize="12"/> 
    <mx:TextInput id="sending_ti" x="148" y="100" width="203" fontSize="12"/> 
    <mx:Label x="105" y="65" text="收到" fontSize="12"/> 
    <mx:Label x="105" y="103" text="發送" fontSize="12"/> 
    <mx:Button x="368" y="64" click="callWrapper();" label="接收" fontSize="12" width="62"/> 
</mx:Application> 


index.html 

<!DOCTYPE HTML PUBLIC "-//W3C//Dtd nowrap HTML 4.01 Transitional//EN"> 
<html> 
<head> 
  <base target="_self"> 
  <title></title> 
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
  <meta http-equiv="pragma" content="no-cache"> 
  <meta http-equiv="cache-control" content="no-cache"> 
  <meta http-equiv="expires" content="0"> 
</head> 

<script language="JavaScript"> 
    function callApp() { 
        var x = MyFlexApp.flexFunctionAlias(); 
        document.getElementById(‘receivedField‘).value = x; 
    } 

    function changeDocumentTitle(a) { 
        window.document.title=a; 
        return document.getElementById(‘sendField‘).value; 
    } 
</script> 

<body style=‘overflow-x:hidden;overflow-y:hidden‘> 
  <form name="htmlForm"> 
     資料發送給AS: 
    <input type="text" id="sendField" /> 
    <input type="button" value="發送" onclick="" /><br /> 
    <br /> 
    接收AS的資料: 
    <input type="text" id="receivedField"> 
    <input type="button" value="接收" onclick="callApp();" /><br />    
  </form> 
   <OBJECT id="MyFlexApp" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0" WIDTH="100%" HEIGHT="500"> 
      <PARAM NAME=movie VALUE="joinJS.swf"> 
      <PARAM NAME=quality VALUE=high> 
      <PARAM NAME=scale VALUE=noborder> 
      <PARAM NAME=bgcolor VALUE=#000000> 
      <EMBED src="joinJS.swf" quality=high WIDTH="100%" HEIGHT="500" scale=noborder bgcolor=#000000 NAME="TH2"TYPE="application/x-shockwave-flash" PLUGINSPAGE="http://www.macromedia.com/go/getflashplayer"></EMBED> 
   </OBJECT> 
</body> 

track:http://www.blogjava.net/Alpha/archive/2009/06/27/284373.html

AS與JS相互連信(Flex中調用js函數)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.