[转]flash 与 js 通讯方法
flash 中调用js 有两种方法,第一中是同getURL 这个方式来调用js,第二种是用 ExternalInterface.call( ).第一种是很常见的用法,我就说一下小明我是怎么用 ExternalInterface.call
什么是 ExternalInterface?
ExternalInterface类是外部api ,也就是actionscript 和 flashplayer 的容器之间实现直接通讯的应用程序编程接口 。在flash的帮助文档里是推荐用ExternalInterface来实现javascript 和 actionscript 之间通讯的。利用ExternalInterface可以调用html网页上任何的js 功能,传递参数并接受来自该调用的返回值。从html 上的js中调用flashplayer中的as 函数。
1 flash 调用 js:
flash部分---------------------------
//应用命名空间;
import flash.external.*;
var greeting:String;//调用createButton 生成一个按钮
var btn:MovieClip = createButton(100, 30, 0xCCCCCC);btn.onPress = function() {
//调用js里的sayHello 函数并接受其返回值
greeting = String(ExternalInterface.call("sayHello", "browser"));
this.mcTxt.text = greeting; // >> Hi Flash.
}
function createButton(width:Number, height:Number, color:Number):MovieClip {
var depth:Number = this.getNextHighestDepth();
var mc:MovieClip = this.createEmptyMovieClip("mc_" + depth, depth);
var mcFmt:TextFormat;
mc.beginFill(color);
mc.lineTo(0, height);
mc.lineTo(width, height);
mc.lineTo(width, 0);
mc.lineTo(0, 0);
mcFmt = new TextFormat();
mcFmt.align = "center";
mcFmt.bold = true;
mc.createTextField("mcTxt", depth, 0, 0, width, height);
mc.mcTxt.text = "Call JS Function";
mc.mcTxt.setTextFormat(mcFmt);
return mc;
}
---------------flash结束js部分-------------------
<script>
function sayHello(name) {
alert(">> Hello " + name + ".");
return ">> Hi Flash.";
}
</script>
---js结束2。js调用 flashflash 部分---
import flash.external.*;
//这个参数是说js里调用as里的函数名
var methodName:String = "goHome";//这个是在该方法中被解析的对象,就是函数里的this 指向谁
var instance:Object = null;//这个是as里的函数名,这个参数就是gohome这个在js里调用的函数名 在as里对应的函数名var method:Function = goTokb;var wasSuccessful:Boolean = ExternalInterface.addCallback(methodName, instance, method);
var txtField:TextField = this.createTextField("txtField", this.getNextHighestDepth(), 0, 0, 200, 50);
txtField.border = true;
txtField.text = wasSuccessful.toString();
function goTokb() {
txtField.text = "http://10kb.net";
getURL("http://www.10kb.net", "_self");
}
------------------------flash 结束js部分-----------------
<form>
<input type="button" onclick="callExternalInterface()" value="Call ExternalInterface" />
</form>
<script>
function callExternalInterface() {
thisMovie("externalInterfaceExample").goHome();
}
function thisMovie(movieName) {
if (navigator.appName.indexOf("Microsoft") != -1) {
return window[movieName]
}
else {
return document[movieName]
}
}
</script>
flash使用的actionscript跟javascript是非常相通的,下面描述如何互相调用函数:
1:javascript调用flash中的函数
在flash的脚本中增加
import flash.external.ExternalInterface;function hello(){
return "hello";
}//第一个参数为导出函数名,第三个参数为as的函数名
这样就可以在js中调用as的hello函数了
2:flash调用js的函数
ExternalInterface.call("hello2", "jacky");
//第一个参数是js的函数名,后面的是js函数的参数
3:如何互相调用
html代码如下:
<object type="application/x-shockwave-flash" data="test.swf" width="525" height="390" name="test">
<param name="allowScriptAccess" value="sameDomain" />
<param name="movie" value="test.swf" />
<param name="quality" value="high" />
<param name="scale" value="noScale" />
<param name="wmode" value="transparent" />
</object>
function callFromFlash() {
var a=thisMovie("test").hello();
alert(a);
}
function thisMovie(movieName) {
if (navigator.appName.indexOf("Microsoft") != -1) {
return window[movieName]
}
else {
return document[movieName]
}
}
//注意,不能使用document.getElementById此类函数取得网页中的flash对象,只能使用thisMovie函数中的代码
浙公网安备 33010602011771号