
Flex中As調(diào)用Js的方法是:
1、導(dǎo)入包 (import flash.external.ExternalInterface;)
2、使用ExternalInterface.call("Js函數(shù)名稱(chēng)",參數(shù))進(jìn)行調(diào)用,其返回的值就是Js函數(shù)所返回的值
Js調(diào)用As的方法是:
1、導(dǎo)入包 (import flash.external.ExternalInterface;)
2、在initApp中使用ExternalInterface.addCallback("用于Js調(diào)用的函數(shù)名",As中的函數(shù)名)進(jìn)行注冊(cè)下
3、js中 就可以用document.getElementById("Flas在Html中的ID").注冊(cè)時(shí)設(shè)置的函數(shù)名(參數(shù))進(jìn)行調(diào)用
as和js通信addcallback失效
參考原文:http://www.zhaohongri.cn/?p=14
情況一:flash一旦在瀏覽器里cache住,如果在as里一開(kāi)始就addcallback就會(huì)失效
情況二:一個(gè)js函數(shù)上來(lái)就調(diào)用as的一個(gè)函數(shù)的時(shí)候,頁(yè)面會(huì)報(bào)錯(cuò),提示找不到這個(gè)flash對(duì)象,或者函數(shù)沒(méi)有定義。Flash8的時(shí)代,針對(duì)
ExternalInterface這個(gè)類(lèi),文檔里只說(shuō)明了怎么用,而沒(méi)有具體說(shuō)怎么合理的組織和頁(yè)面的結(jié)構(gòu),一直到了cs3的時(shí)代,幫助里才說(shuō)明了正確
的函數(shù)注冊(cè)和js調(diào)用的過(guò)程,具體的見(jiàn)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函數(shù)里用了 return window[movieName+”_ob”]和return document[movieName+”_em”],在IE下,如果object標(biāo)簽和embed表現(xiàn)用同樣的id,通過(guò)js去訪問(wèn)flash對(duì)象的時(shí) 候,IE會(huì)認(rèn)不出,F(xiàn)F是沒(méi)有問(wèn)題的
as部分
private function registerJsFun():void{
if(ExternalInterface.available){
try{
var
containerReady:Boolean=isContainerReady();
//ExternalInterface.call(”ceshi”,”registerJsFun:”+containerReady);
if(containerReady){
//注冊(cè)函數(shù)
setupCallBacks();
}else{
//檢測(cè)是否準(zhǔn)備好
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”);
}
具體我就不解釋了,不明白的可以仔細(xì)去看下cs3幫助,大概的意思就是頁(yè)面開(kāi)始渲染的時(shí)候js去調(diào)用swf對(duì)象,有可能swf對(duì)象沒(méi)有完全 load完,所以這個(gè)觸發(fā)器要從flash開(kāi)始,當(dāng)flash加載的時(shí)候就開(kāi)始不停的調(diào)用頁(yè)面的一個(gè)函數(shù),取一個(gè)頁(yè)面是否加載完畢的標(biāo)識(shí),當(dāng) pageonLoad后,這個(gè)標(biāo)識(shí)為true了,說(shuō)明flash也加載完畢了,這個(gè)時(shí)候flash再開(kāi)始注冊(cè)函數(shù),同時(shí)調(diào)用頁(yè)面的js,讓js調(diào)用 Flash對(duì)象
實(shí)例:a.mxml
<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調(diào)用flash時(shí)間上函數(shù)
ExternalInterface.addCallback("flexFunctionAlias", asFunc);
}
public function callWrapper():void {
var f:String = "changeDocumentTitle";
//ExternalInterface.call(functionName:String,Parameters)可以調(diào)用javascript函數(shù)
//參數(shù)1: functionName – 你想要調(diào)用的javascript函數(shù)名要以字符串的形式
//參數(shù)2: Parameters – 需要傳遞給javascript函數(shù)的參數(shù),用逗號(hào)分開(kāi),是可選的。
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="發(fā)送" 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="發(fā)送" fontSize="12"/>
<mx:Button x="368" y="64" click="callWrapper();" label="接收" fontSize="12" width="62"/>
</mx:Application>
index.html
<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">
數(shù)據(jù)發(fā)送給AS:
<input type="text" id="sendField" />
<input type="button" value="發(fā)送" onclick="" /><br />
<br />
接收AS的數(shù)據(jù):
<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>
