js调用as
在flex中进行注册
<fx:Script>
<![CDATA[
import mx.controls.Alert;
import mx.events.FlexEvent;
public var sound:Sound;
protected function play(url:String,stop:String,go:String):void // 调用函数
{
//”http://192.168.10.104:8080/group1/M00/00/00/wKgKaVFx-8m6PEtTAEjMrhpBa1U552.mp3″
var req:URLRequest = new URLRequest(url);
var context:SoundLoaderContext = new SoundLoaderContext(1000, true);
sound = new Sound(req,context);
sound.addEventListener(Event.COMPLETE,playSound);
}
public function playSound(e:Event):void{
sound.play();
}
protected function initApp(event:FlexEvent):void
{
ExternalInterface.addCallback(“play”,play); // 注册
}
]]>
</fx:Script>
js中调用该注册函数
function playVoice(url,stop,go){
var flash = frame.window.document.getElementById(“voicePlay”);
flash.play(url,stop,go);
}
as调用js
as处
protected function initApp(event:FlexEvent):void
{
var url:String = ExternalInterface.call(“getUrl”); //调用
ecgService.url = url;
ecgService.send();
ecg = new Ecg(new Array(),0,0);
}
js处
<script type=”text/javascript”>
function getUrl(){
<% StringBuffer url = request.getRequestURL();
String tempContextUrl = url.delete(url.length() – request.getRequestURI().length(), url.length()).append(request.getContextPath()).append(“/”).toString();
%> //JSP代码
var url = “<%=tempContextUrl+”lastEcg.action”%>”;
return url;
}
</script>
若用iframe内嵌则
var flash = frame.window.document.getElementById(“appName”); frame为内嵌frame的名字
var parent =
window.parent
js与as的安全交互必须满足:
-
<param name=’allowScriptAccess’ value =’always’ />
-
flash不能隐藏(display:none)
-
等被调用方初始化完成再去调用,as中可以用ExtercalInterface.call(‘flashready’)来告知初始化完成
-
跨域执行,必须在flash中设置
Secure.allowDomain或者Secure.allowInsecureDomain
http://12d.iteye.com/blog/1567416