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部分:
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"];
}
}
οnlοad=function(){
pageInit();
}
注意,在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,timeH andler);
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("isR eady"));
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
index.html
function callApp() {
var x = MyFlexApp.flexFunctionAlias();
document.getElementById('receivedField').value = x;
}
function changeDocumentTitle(a) {
window.document.title=a;
return document.getElementById('sendField').value;
}
数据发送给AS:
接收AS的数据:
本文来自优快云博客,转载请标明出处:http://blog.youkuaiyun.com/hartkevin/archive/2010/03/10/ 5361524.aspx
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部分:
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"];
}
}
οnlοad=function(){
pageInit();
}
注意,在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,timeH andler);
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("isR eady"));
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
index.html
function callApp() {
var x = MyFlexApp.flexFunctionAlias();
document.getElementById('receivedField').value = x;
}
function changeDocumentTitle(a) {
window.document.title=a;
return document.getElementById('sendField').value;
}
数据发送给AS:
接收AS的数据:
本文来自优快云博客,转载请标明出处:http://blog.youkuaiyun.com/hartkevin/archive/2010/03/10/ 5361524.aspx